

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>typing &mdash; argoverse  documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/graphviz.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> argoverse
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">argoverse</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>typing</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for typing</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">abc</span>
<span class="kn">from</span> <span class="nn">abc</span> <span class="k">import</span> <span class="n">abstractmethod</span><span class="p">,</span> <span class="n">abstractproperty</span>
<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">contextlib</span>
<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">re</span> <span class="k">as</span> <span class="nn">stdlib_re</span>  <span class="c1"># Avoid confusion with the re we export.</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">types</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">collections.abc</span> <span class="k">as</span> <span class="nn">collections_abc</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">collections</span> <span class="k">as</span> <span class="nn">collections_abc</span>  <span class="c1"># Fallback for PY3.2.</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">):</span>
    <span class="kn">import</span> <span class="nn">_collections_abc</span>  <span class="c1"># Needed for private function _check_methods # noqa</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">types</span> <span class="k">import</span> <span class="n">WrapperDescriptorType</span><span class="p">,</span> <span class="n">MethodWrapperType</span><span class="p">,</span> <span class="n">MethodDescriptorType</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">WrapperDescriptorType</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">object</span><span class="o">.</span><span class="fm">__init__</span><span class="p">)</span>
    <span class="n">MethodWrapperType</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">object</span><span class="p">()</span><span class="o">.</span><span class="fm">__str__</span><span class="p">)</span>
    <span class="n">MethodDescriptorType</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="o">.</span><span class="n">join</span><span class="p">)</span>


<span class="c1"># Please keep __all__ alphabetized within each category.</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1"># Super-special typing primitives.</span>
    <span class="s1">&#39;Any&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Callable&#39;</span><span class="p">,</span>
    <span class="s1">&#39;ClassVar&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Generic&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Optional&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Tuple&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Type&#39;</span><span class="p">,</span>
    <span class="s1">&#39;TypeVar&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Union&#39;</span><span class="p">,</span>

    <span class="c1"># ABCs (from collections.abc).</span>
    <span class="s1">&#39;AbstractSet&#39;</span><span class="p">,</span>  <span class="c1"># collections.abc.Set.</span>
    <span class="s1">&#39;GenericMeta&#39;</span><span class="p">,</span>  <span class="c1"># subclass of abc.ABCMeta and a metaclass</span>
                    <span class="c1"># for &#39;Generic&#39; and ABCs below.</span>
    <span class="s1">&#39;ByteString&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Container&#39;</span><span class="p">,</span>
    <span class="s1">&#39;ContextManager&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Hashable&#39;</span><span class="p">,</span>
    <span class="s1">&#39;ItemsView&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Iterable&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Iterator&#39;</span><span class="p">,</span>
    <span class="s1">&#39;KeysView&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Mapping&#39;</span><span class="p">,</span>
    <span class="s1">&#39;MappingView&#39;</span><span class="p">,</span>
    <span class="s1">&#39;MutableMapping&#39;</span><span class="p">,</span>
    <span class="s1">&#39;List&#39;</span><span class="p">,</span>
    <span class="s1">&#39;MutableSet&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Sequence&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Sized&#39;</span><span class="p">,</span>
    <span class="s1">&#39;ValuesView&#39;</span><span class="p">,</span>
    <span class="c1"># The following are added depending on presence</span>
    <span class="c1"># of their non-generic counterparts in stdlib:</span>
    <span class="c1"># Awaitable,</span>
    <span class="c1"># AsyncIterator,</span>
    <span class="c1"># AsyncIterable,</span>
    <span class="c1"># Coroutine,</span>
    <span class="c1"># Collection,</span>
    <span class="c1"># AsyncGenerator,</span>
    <span class="c1"># AsyncContextManager</span>

    <span class="c1"># Structural checks, a.k.a. protocols.</span>
    <span class="s1">&#39;Reversible&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SupportsAbs&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SupportsBytes&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SupportsComplex&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SupportsFloat&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SupportsInt&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SupportsRound&#39;</span><span class="p">,</span>

    <span class="c1"># Concrete collection types.</span>
    <span class="s1">&#39;Counter&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Deque&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Dict&#39;</span><span class="p">,</span>
    <span class="s1">&#39;DefaultDict&#39;</span><span class="p">,</span>
    <span class="s1">&#39;List&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Set&#39;</span><span class="p">,</span>
    <span class="s1">&#39;FrozenSet&#39;</span><span class="p">,</span>
    <span class="s1">&#39;NamedTuple&#39;</span><span class="p">,</span>  <span class="c1"># Not really a type.</span>
    <span class="s1">&#39;Generator&#39;</span><span class="p">,</span>

    <span class="c1"># One-off things.</span>
    <span class="s1">&#39;AnyStr&#39;</span><span class="p">,</span>
    <span class="s1">&#39;cast&#39;</span><span class="p">,</span>
    <span class="s1">&#39;get_type_hints&#39;</span><span class="p">,</span>
    <span class="s1">&#39;NewType&#39;</span><span class="p">,</span>
    <span class="s1">&#39;no_type_check&#39;</span><span class="p">,</span>
    <span class="s1">&#39;no_type_check_decorator&#39;</span><span class="p">,</span>
    <span class="s1">&#39;overload&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Text&#39;</span><span class="p">,</span>
    <span class="s1">&#39;TYPE_CHECKING&#39;</span><span class="p">,</span>
<span class="p">]</span>

<span class="c1"># The pseudo-submodules &#39;re&#39; and &#39;io&#39; are part of the public</span>
<span class="c1"># namespace, but excluded from __all__ because they might stomp on</span>
<span class="c1"># legitimate imports of those modules.</span>


<span class="k">def</span> <span class="nf">_qualname</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="vm">__qualname__</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Fall back to just name.</span>
        <span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="vm">__name__</span>


<span class="k">def</span> <span class="nf">_trim_name</span><span class="p">(</span><span class="n">nm</span><span class="p">):</span>
    <span class="n">whitelist</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_TypeAlias&#39;</span><span class="p">,</span> <span class="s1">&#39;_ForwardRef&#39;</span><span class="p">,</span> <span class="s1">&#39;_TypingBase&#39;</span><span class="p">,</span> <span class="s1">&#39;_FinalTypingBase&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">nm</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">nm</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">whitelist</span><span class="p">:</span>
        <span class="n">nm</span> <span class="o">=</span> <span class="n">nm</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
    <span class="k">return</span> <span class="n">nm</span>


<span class="k">class</span> <span class="nc">TypingMeta</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Metaclass for most types defined in typing module</span>
<span class="sd">    (not a part of public API).</span>

<span class="sd">    This overrides __new__() to require an extra keyword parameter</span>
<span class="sd">    &#39;_root&#39;, which serves as a guard against naive subclassing of the</span>
<span class="sd">    typing classes.  Any legitimate class defined using a metaclass</span>
<span class="sd">    derived from TypingMeta must pass _root=True.</span>

<span class="sd">    This also defines a dummy constructor (all the work for most typing</span>
<span class="sd">    constructs is done in __new__) and a nicer repr().</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_is_protocol</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">_root</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot subclass </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">_type_repr</span><span class="p">,</span> <span class="n">bases</span><span class="p">))</span> <span class="ow">or</span> <span class="s1">&#39;()&#39;</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Override this in subclasses to interpret forward references.</span>

<span class="sd">        For example, List[&#39;C&#39;] is internally stored as</span>
<span class="sd">        List[_ForwardRef(&#39;C&#39;)], which should evaluate to List[C],</span>
<span class="sd">        where C is an object found in globalns or localns (searching</span>
<span class="sd">        localns first, of course).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">_get_type_vars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">qname</span> <span class="o">=</span> <span class="n">_trim_name</span><span class="p">(</span><span class="n">_qualname</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span> <span class="n">qname</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_TypingBase</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">TypingMeta</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal indicator of special typing constructs.&quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__weakref__&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor.</span>

<span class="sd">        This only exists to give a better error message in case</span>
<span class="sd">        someone tries to subclass a special typing object (not a good idea).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span>
                <span class="nb">isinstance</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">str</span><span class="p">)</span> <span class="ow">and</span>
                <span class="nb">isinstance</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="c1"># Close enough.</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot subclass </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">cls</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>

    <span class="c1"># Things that are not classes also need these.</span>
    <span class="k">def</span> <span class="nf">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">_get_type_vars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">cls</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">qname</span> <span class="o">=</span> <span class="n">_trim_name</span><span class="p">(</span><span class="n">_qualname</span><span class="p">(</span><span class="bp">cls</span><span class="p">))</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span> <span class="n">qname</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot instantiate </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>


<span class="k">class</span> <span class="nc">_FinalTypingBase</span><span class="p">(</span><span class="n">_TypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal mix-in class to prevent instantiation.</span>

<span class="sd">    Prevents instantiation unless _root=True is given in class call.</span>
<span class="sd">    It is used to create pseudo-singleton instances Any, Union, Optional, etc.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="bp">self</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">_root</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot instantiate </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">cls</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__reduce__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_trim_name</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_ForwardRef</span><span class="p">(</span><span class="n">_TypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal wrapper to hold a forward reference.&quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__forward_arg__&#39;</span><span class="p">,</span> <span class="s1">&#39;__forward_code__&#39;</span><span class="p">,</span>
                 <span class="s1">&#39;__forward_evaluated__&#39;</span><span class="p">,</span> <span class="s1">&#39;__forward_value__&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Forward reference must be a string -- got </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">arg</span><span class="p">,))</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">code</span> <span class="o">=</span> <span class="nb">compile</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="s1">&#39;&lt;string&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;eval&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">SyntaxError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">SyntaxError</span><span class="p">(</span><span class="s1">&#39;Forward reference must be an expression -- got </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span>
                              <span class="p">(</span><span class="n">arg</span><span class="p">,))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__forward_arg__</span> <span class="o">=</span> <span class="n">arg</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__forward_code__</span> <span class="o">=</span> <span class="n">code</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__forward_evaluated__</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__forward_value__</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">__forward_evaluated__</span> <span class="ow">or</span> <span class="n">localns</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">globalns</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">globalns</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">localns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">globalns</span> <span class="o">=</span> <span class="n">localns</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">elif</span> <span class="n">globalns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">globalns</span> <span class="o">=</span> <span class="n">localns</span>
            <span class="k">elif</span> <span class="n">localns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">localns</span> <span class="o">=</span> <span class="n">globalns</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__forward_value__</span> <span class="o">=</span> <span class="n">_type_check</span><span class="p">(</span>
                <span class="nb">eval</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__forward_code__</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">),</span>
                <span class="s2">&quot;Forward references must evaluate to types.&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__forward_evaluated__</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__forward_value__</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">_ForwardRef</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__forward_arg__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">__forward_arg__</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__forward_value__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">__forward_value__</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hash</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">__forward_arg__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__forward_value__</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Forward references cannot be used with isinstance().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Forward references cannot be used with issubclass().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;_ForwardRef(</span><span class="si">%r</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__forward_arg__</span><span class="p">,)</span>


<span class="k">class</span> <span class="nc">_TypeAlias</span><span class="p">(</span><span class="n">_TypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal helper class for defining generic variants of concrete types.</span>

<span class="sd">    Note that this is not a type; let&#39;s call it a pseudo-type.  It cannot</span>
<span class="sd">    be used in instance and subclass checks in parameterized form, i.e.</span>
<span class="sd">    ``isinstance(42, Match[str])`` raises ``TypeError`` instead of returning</span>
<span class="sd">    ``False``.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;type_var&#39;</span><span class="p">,</span> <span class="s1">&#39;impl_type&#39;</span><span class="p">,</span> <span class="s1">&#39;type_checker&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">type_var</span><span class="p">,</span> <span class="n">impl_type</span><span class="p">,</span> <span class="n">type_checker</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initializer.</span>

<span class="sd">        Args:</span>
<span class="sd">            name: The name, e.g. &#39;Pattern&#39;.</span>
<span class="sd">            type_var: The type parameter, e.g. AnyStr, or the</span>
<span class="sd">                specific type, e.g. str.</span>
<span class="sd">            impl_type: The implementation type.</span>
<span class="sd">            type_checker: Function that takes an impl_type instance.</span>
<span class="sd">                and returns a value that should be a type_var instance.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="nb">str</span><span class="p">),</span> <span class="nb">repr</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">impl_type</span><span class="p">,</span> <span class="nb">type</span><span class="p">),</span> <span class="nb">repr</span><span class="p">(</span><span class="n">impl_type</span><span class="p">)</span>
        <span class="k">assert</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">impl_type</span><span class="p">,</span> <span class="n">TypingMeta</span><span class="p">),</span> <span class="nb">repr</span><span class="p">(</span><span class="n">impl_type</span><span class="p">)</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">type_var</span><span class="p">,</span> <span class="p">(</span><span class="nb">type</span><span class="p">,</span> <span class="n">_TypingBase</span><span class="p">)),</span> <span class="nb">repr</span><span class="p">(</span><span class="n">type_var</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">type_var</span> <span class="o">=</span> <span class="n">type_var</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">impl_type</span> <span class="o">=</span> <span class="n">impl_type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">type_checker</span> <span class="o">=</span> <span class="n">type_checker</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">[</span><span class="si">%s</span><span class="s2">]&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">_type_repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parameter</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="p">,</span> <span class="n">TypeVar</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> cannot be further parameterized.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="o">.</span><span class="n">__constraints__</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parameter</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">parameter</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="o">.</span><span class="n">__constraints__</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> is not a valid substitution for </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span>
                                <span class="p">(</span><span class="n">parameter</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="p">))</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parameter</span><span class="p">,</span> <span class="n">TypeVar</span><span class="p">)</span> <span class="ow">and</span> <span class="n">parameter</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> cannot be re-parameterized.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">parameter</span><span class="p">,</span>
                              <span class="bp">self</span><span class="o">.</span><span class="n">impl_type</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">type_checker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">_TypeAlias</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">name</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">type_var</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">type_var</span>

    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hash</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="p">,</span> <span class="n">TypeVar</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Parameterized type aliases cannot be used &quot;</span>
                            <span class="s2">&quot;with isinstance().&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">impl_type</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">type_var</span><span class="p">,</span> <span class="n">TypeVar</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Parameterized type aliases cannot be used &quot;</span>
                            <span class="s2">&quot;with issubclass().&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">issubclass</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">impl_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_get_type_vars</span><span class="p">(</span><span class="n">types</span><span class="p">,</span> <span class="n">tvars</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">types</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">TypingMeta</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">_TypingBase</span><span class="p">):</span>
            <span class="n">t</span><span class="o">.</span><span class="n">_get_type_vars</span><span class="p">(</span><span class="n">tvars</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_type_vars</span><span class="p">(</span><span class="n">types</span><span class="p">):</span>
    <span class="n">tvars</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">_get_type_vars</span><span class="p">(</span><span class="n">types</span><span class="p">,</span> <span class="n">tvars</span><span class="p">)</span>
    <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">tvars</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_eval_type</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">TypingMeta</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">_TypingBase</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">t</span><span class="o">.</span><span class="n">_eval_type</span><span class="p">(</span><span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">t</span>


<span class="k">def</span> <span class="nf">_type_check</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Check that the argument is a type, and return it (internal helper).</span>

<span class="sd">    As a special case, accept None and return type(None) instead.</span>
<span class="sd">    Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.</span>

<span class="sd">    The msg argument is a human-readable error message, e.g.</span>

<span class="sd">        &quot;Union[arg, ...]: arg should be a type.&quot;</span>

<span class="sd">    We append the repr() of the actual value (truncated to 100 chars).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">arg</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">arg</span> <span class="o">=</span> <span class="n">_ForwardRef</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
    <span class="k">if</span> <span class="p">(</span>
        <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">_TypingBase</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">type</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;_ClassVar&#39;</span> <span class="ow">or</span>
        <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="p">(</span><span class="nb">type</span><span class="p">,</span> <span class="n">_TypingBase</span><span class="p">))</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">callable</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
    <span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="n">msg</span> <span class="o">+</span> <span class="s2">&quot; Got </span><span class="si">%.100r</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">arg</span><span class="p">,))</span>
    <span class="c1"># Bare Union etc. are not valid as type arguments</span>
    <span class="k">if</span> <span class="p">(</span>
        <span class="nb">type</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;_Union&#39;</span><span class="p">,</span> <span class="s1">&#39;_Optional&#39;</span><span class="p">)</span> <span class="ow">and</span>
        <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span>
        <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">TypingMeta</span><span class="p">)</span> <span class="ow">and</span> <span class="n">arg</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">in</span> <span class="p">(</span><span class="n">Generic</span><span class="p">,</span> <span class="n">_Protocol</span><span class="p">)</span>
    <span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Plain </span><span class="si">%s</span><span class="s2"> is not valid as type argument&quot;</span> <span class="o">%</span> <span class="n">arg</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">arg</span>


<span class="k">def</span> <span class="nf">_type_repr</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return the repr() of an object, special-casing types (internal helper).</span>

<span class="sd">    If obj is a type, we return a shorter version than the default</span>
<span class="sd">    type.__repr__, based on the module and qualified name, which is</span>
<span class="sd">    typically enough to uniquely identify a type.  For everything</span>
<span class="sd">    else, we fall back on repr(obj).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">TypingMeta</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">obj</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="s1">&#39;builtins&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">_qualname</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span> <span class="n">_qualname</span><span class="p">(</span><span class="n">obj</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="o">...</span><span class="p">:</span>
        <span class="k">return</span><span class="p">(</span><span class="s1">&#39;...&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">FunctionType</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="vm">__name__</span>
    <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_Any</span><span class="p">(</span><span class="n">_FinalTypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Special type indicating an unconstrained type.</span>

<span class="sd">    - Any is compatible with every type.</span>
<span class="sd">    - Any assumed to have all methods.</span>
<span class="sd">    - All values assumed to be instances of Any.</span>

<span class="sd">    Note that all the above statements are true from the point of view of</span>
<span class="sd">    static type checkers. At runtime, Any should not be used with instance</span>
<span class="sd">    or class checks.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Any cannot be used with isinstance().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Any cannot be used with issubclass().&quot;</span><span class="p">)</span>


<span class="n">Any</span> <span class="o">=</span> <span class="n">_Any</span><span class="p">(</span><span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_NoReturn</span><span class="p">(</span><span class="n">_FinalTypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Special type indicating functions that never return.</span>
<span class="sd">    Example::</span>

<span class="sd">      from typing import NoReturn</span>

<span class="sd">      def stop() -&gt; NoReturn:</span>
<span class="sd">          raise Exception(&#39;no way&#39;)</span>

<span class="sd">    This type is invalid in other positions, e.g., ``List[NoReturn]``</span>
<span class="sd">    will fail in static type checkers.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;NoReturn cannot be used with isinstance().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;NoReturn cannot be used with issubclass().&quot;</span><span class="p">)</span>


<span class="n">NoReturn</span> <span class="o">=</span> <span class="n">_NoReturn</span><span class="p">(</span><span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">TypeVar</span><span class="p">(</span><span class="n">_TypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Type variable.</span>

<span class="sd">    Usage::</span>

<span class="sd">      T = TypeVar(&#39;T&#39;)  # Can be anything</span>
<span class="sd">      A = TypeVar(&#39;A&#39;, str, bytes)  # Must be str or bytes</span>

<span class="sd">    Type variables exist primarily for the benefit of static type</span>
<span class="sd">    checkers.  They serve as the parameters for generic types as well</span>
<span class="sd">    as for generic function definitions.  See class Generic for more</span>
<span class="sd">    information on generic types.  Generic functions work as follows:</span>

<span class="sd">      def repeat(x: T, n: int) -&gt; List[T]:</span>
<span class="sd">          &#39;&#39;&#39;Return a list containing n references to x.&#39;&#39;&#39;</span>
<span class="sd">          return [x]*n</span>

<span class="sd">      def longest(x: A, y: A) -&gt; A:</span>
<span class="sd">          &#39;&#39;&#39;Return the longest of two strings.&#39;&#39;&#39;</span>
<span class="sd">          return x if len(x) &gt;= len(y) else y</span>

<span class="sd">    The latter example&#39;s signature is essentially the overloading</span>
<span class="sd">    of (str, str) -&gt; str and (bytes, bytes) -&gt; bytes.  Also note</span>
<span class="sd">    that if the arguments are instances of some subclass of str,</span>
<span class="sd">    the return type is still plain str.</span>

<span class="sd">    At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.</span>

<span class="sd">    Type variables defined with covariant=True or contravariant=True</span>
<span class="sd">    can be used do declare covariant or contravariant generic types.</span>
<span class="sd">    See PEP 484 for more details. By default generic types are invariant</span>
<span class="sd">    in all type variables.</span>

<span class="sd">    Type variables can be introspected. e.g.:</span>

<span class="sd">      T.__name__ == &#39;T&#39;</span>
<span class="sd">      T.__constraints__ == ()</span>
<span class="sd">      T.__covariant__ == False</span>
<span class="sd">      T.__contravariant__ = False</span>
<span class="sd">      A.__constraints__ == (str, bytes)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__name__&#39;</span><span class="p">,</span> <span class="s1">&#39;__bound__&#39;</span><span class="p">,</span> <span class="s1">&#39;__constraints__&#39;</span><span class="p">,</span>
                 <span class="s1">&#39;__covariant__&#39;</span><span class="p">,</span> <span class="s1">&#39;__contravariant__&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">constraints</span><span class="p">,</span> <span class="n">bound</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">covariant</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">contravariant</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">constraints</span><span class="p">,</span> <span class="n">bound</span><span class="o">=</span><span class="n">bound</span><span class="p">,</span>
                         <span class="n">covariant</span><span class="o">=</span><span class="n">covariant</span><span class="p">,</span> <span class="n">contravariant</span><span class="o">=</span><span class="n">contravariant</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">=</span> <span class="n">name</span>
        <span class="k">if</span> <span class="n">covariant</span> <span class="ow">and</span> <span class="n">contravariant</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Bivariant types are not supported.&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__covariant__</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="n">covariant</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__contravariant__</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="n">contravariant</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">constraints</span> <span class="ow">and</span> <span class="n">bound</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Constraints cannot be combined with bound=...&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">constraints</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraints</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;A single constraint is not allowed&quot;</span><span class="p">)</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;TypeVar(name, constraint, ...): constraints must be types.&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__constraints__</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">_type_check</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">constraints</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">bound</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__bound__</span> <span class="o">=</span> <span class="n">_type_check</span><span class="p">(</span><span class="n">bound</span><span class="p">,</span> <span class="s2">&quot;Bound must be a type.&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__bound__</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_get_type_vars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tvars</span><span class="p">:</span>
            <span class="n">tvars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__covariant__</span><span class="p">:</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="s1">&#39;+&#39;</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">__contravariant__</span><span class="p">:</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="s1">&#39;-&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="s1">&#39;~&#39;</span>
        <span class="k">return</span> <span class="n">prefix</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__name__</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type variables cannot be used with isinstance().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type variables cannot be used with issubclass().&quot;</span><span class="p">)</span>


<span class="c1"># Some unconstrained type variables.  These are used by the container types.</span>
<span class="c1"># (These are not for export.)</span>
<span class="n">T</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;T&#39;</span><span class="p">)</span>  <span class="c1"># Any type.</span>
<span class="n">KT</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;KT&#39;</span><span class="p">)</span>  <span class="c1"># Key type.</span>
<span class="n">VT</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;VT&#39;</span><span class="p">)</span>  <span class="c1"># Value type.</span>
<span class="n">T_co</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;T_co&#39;</span><span class="p">,</span> <span class="n">covariant</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>  <span class="c1"># Any type covariant containers.</span>
<span class="n">V_co</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;V_co&#39;</span><span class="p">,</span> <span class="n">covariant</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>  <span class="c1"># Any type covariant containers.</span>
<span class="n">VT_co</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;VT_co&#39;</span><span class="p">,</span> <span class="n">covariant</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>  <span class="c1"># Value type covariant containers.</span>
<span class="n">T_contra</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;T_contra&#39;</span><span class="p">,</span> <span class="n">contravariant</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>  <span class="c1"># Ditto contravariant.</span>

<span class="c1"># A useful type variable with constraints.  This represents string types.</span>
<span class="c1"># (This one *is* for export!)</span>
<span class="n">AnyStr</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;AnyStr&#39;</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_replace_arg</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">tvars</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;An internal helper function: replace arg if it is a type variable</span>
<span class="sd">    found in tvars with corresponding substitution from args or</span>
<span class="sd">    with corresponding substitution sub-tree if arg is a generic type.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">tvars</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">tvars</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="s1">&#39;_subs_tree&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="p">(</span><span class="n">GenericMeta</span><span class="p">,</span> <span class="n">_TypingBase</span><span class="p">)):</span>
        <span class="k">return</span> <span class="n">arg</span><span class="o">.</span><span class="n">_subs_tree</span><span class="p">(</span><span class="n">tvars</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">TypeVar</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">tvar</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">tvars</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">arg</span> <span class="o">==</span> <span class="n">tvar</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">args</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">arg</span>


<span class="c1"># Special typing constructs Union, Optional, Generic, Callable and Tuple</span>
<span class="c1"># use three special attributes for internal bookkeeping of generic types:</span>
<span class="c1"># * __parameters__ is a tuple of unique free type parameters of a generic</span>
<span class="c1">#   type, for example, Dict[T, T].__parameters__ == (T,);</span>
<span class="c1"># * __origin__ keeps a reference to a type that was subscripted,</span>
<span class="c1">#   e.g., Union[T, int].__origin__ == Union;</span>
<span class="c1"># * __args__ is a tuple of all arguments used in subscripting,</span>
<span class="c1">#   e.g., Dict[T, int].__args__ == (T, int).</span>


<span class="k">def</span> <span class="nf">_subs_tree</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">tvars</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;An internal helper function: calculate substitution tree</span>
<span class="sd">    for generic cls after replacing its type parameters with</span>
<span class="sd">    substitutions in tvars -&gt; args (if any).</span>
<span class="sd">    Repeat the same following __origin__&#39;s.</span>

<span class="sd">    Return a list of arguments with all possible substitutions</span>
<span class="sd">    performed. Arguments that are generic classes themselves are represented</span>
<span class="sd">    as tuples (so that no new classes are created by this function).</span>
<span class="sd">    For example: _subs_tree(List[Tuple[int, T]][str]) == [(Tuple, int, str)]</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">cls</span>
    <span class="c1"># Make of chain of origins (i.e. cls -&gt; cls.__origin__)</span>
    <span class="n">current</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__origin__</span>
    <span class="n">orig_chain</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">while</span> <span class="n">current</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">orig_chain</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current</span><span class="p">)</span>
        <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">__origin__</span>
    <span class="c1"># Replace type variables in __args__ if asked ...</span>
    <span class="n">tree_args</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__args__</span><span class="p">:</span>
        <span class="n">tree_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_replace_arg</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">tvars</span><span class="p">,</span> <span class="n">args</span><span class="p">))</span>
    <span class="c1"># ... then continue replacing down the origin chain.</span>
    <span class="k">for</span> <span class="n">ocls</span> <span class="ow">in</span> <span class="n">orig_chain</span><span class="p">:</span>
        <span class="n">new_tree_args</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">ocls</span><span class="o">.</span><span class="n">__args__</span><span class="p">:</span>
            <span class="n">new_tree_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_replace_arg</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">ocls</span><span class="o">.</span><span class="n">__parameters__</span><span class="p">,</span> <span class="n">tree_args</span><span class="p">))</span>
        <span class="n">tree_args</span> <span class="o">=</span> <span class="n">new_tree_args</span>
    <span class="k">return</span> <span class="n">tree_args</span>


<span class="k">def</span> <span class="nf">_remove_dups_flatten</span><span class="p">(</span><span class="n">parameters</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;An internal helper for Union creation and substitution: flatten Union&#39;s</span>
<span class="sd">    among parameters, then remove duplicates and strict subclasses.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Flatten out Union[Union[...], ...].</span>
    <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parameters</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">_Union</span><span class="p">)</span> <span class="ow">and</span> <span class="n">p</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="n">Union</span><span class="p">:</span>
            <span class="n">params</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">__args__</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">is</span> <span class="n">Union</span><span class="p">:</span>
            <span class="n">params</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
    <span class="c1"># Weed out strict duplicates, preserving the first of each occurrence.</span>
    <span class="n">all_params</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">all_params</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">params</span><span class="p">):</span>
        <span class="n">new_params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">params</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">all_params</span><span class="p">:</span>
                <span class="n">new_params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
                <span class="n">all_params</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="n">new_params</span>
        <span class="k">assert</span> <span class="ow">not</span> <span class="n">all_params</span><span class="p">,</span> <span class="n">all_params</span>
    <span class="c1"># Weed out subclasses.</span>
    <span class="c1"># E.g. Union[int, Employee, Manager] == Union[int, Employee].</span>
    <span class="c1"># If object is present it will be sole survivor among proper classes.</span>
    <span class="c1"># Never discard type variables.</span>
    <span class="c1"># (In particular, Union[str, AnyStr] != AnyStr.)</span>
    <span class="n">all_params</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">t1</span> <span class="ow">in</span> <span class="n">params</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">t2</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span> <span class="n">t2</span><span class="p">)</span>
               <span class="k">for</span> <span class="n">t2</span> <span class="ow">in</span> <span class="n">all_params</span> <span class="o">-</span> <span class="p">{</span><span class="n">t1</span><span class="p">}</span>
               <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">t2</span><span class="p">,</span> <span class="n">GenericMeta</span><span class="p">)</span> <span class="ow">and</span>
                       <span class="n">t2</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">)):</span>
            <span class="n">all_params</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">t1</span><span class="p">)</span>
    <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">params</span> <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">all_params</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_check_generic</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
    <span class="c1"># Check correct count for parameters of a generic cls (internal helper).</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__parameters__</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> is not a generic class&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">cls</span><span class="p">))</span>
    <span class="n">alen</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>
    <span class="n">elen</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">__parameters__</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">alen</span> <span class="o">!=</span> <span class="n">elen</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Too </span><span class="si">%s</span><span class="s2"> parameters for </span><span class="si">%s</span><span class="s2">; actual </span><span class="si">%s</span><span class="s2">, expected </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="s2">&quot;many&quot;</span> <span class="k">if</span> <span class="n">alen</span> <span class="o">&gt;</span> <span class="n">elen</span> <span class="k">else</span> <span class="s2">&quot;few&quot;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">cls</span><span class="p">),</span> <span class="n">alen</span><span class="p">,</span> <span class="n">elen</span><span class="p">))</span>


<span class="n">_cleanups</span> <span class="o">=</span> <span class="p">[]</span>


<span class="k">def</span> <span class="nf">_tp_cache</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal wrapper caching __getitem__ of generic types with a fallback to</span>
<span class="sd">    original function for non-hashable arguments.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">cached</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">()(</span><span class="n">func</span><span class="p">)</span>
    <span class="n">_cleanups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cached</span><span class="o">.</span><span class="n">cache_clear</span><span class="p">)</span>

    <span class="nd">@functools</span><span class="o">.</span><span class="n">wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">cached</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
            <span class="k">pass</span>  <span class="c1"># All real errors (not unhashable args) are raised below.</span>
        <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">inner</span>


<span class="k">class</span> <span class="nc">_Union</span><span class="p">(</span><span class="n">_FinalTypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Union type; Union[X, Y] means either X or Y.</span>

<span class="sd">    To define a union, use e.g. Union[int, str].  Details:</span>

<span class="sd">    - The arguments must be types and there must be at least one.</span>

<span class="sd">    - None as an argument is a special case and is replaced by</span>
<span class="sd">      type(None).</span>

<span class="sd">    - Unions of unions are flattened, e.g.::</span>

<span class="sd">        Union[Union[int, str], float] == Union[int, str, float]</span>

<span class="sd">    - Unions of a single argument vanish, e.g.::</span>

<span class="sd">        Union[int] == int  # The constructor actually returns int</span>

<span class="sd">    - Redundant arguments are skipped, e.g.::</span>

<span class="sd">        Union[int, str, int] == Union[int, str]</span>

<span class="sd">    - When comparing unions, the argument order is ignored, e.g.::</span>

<span class="sd">        Union[int, str] == Union[str, int]</span>

<span class="sd">    - When two arguments have a subclass relationship, the least</span>
<span class="sd">      derived argument is kept, e.g.::</span>

<span class="sd">        class Employee: pass</span>
<span class="sd">        class Manager(Employee): pass</span>
<span class="sd">        Union[int, Employee, Manager] == Union[int, Employee]</span>
<span class="sd">        Union[Manager, int, Employee] == Union[int, Employee]</span>
<span class="sd">        Union[Employee, Manager] == Employee</span>

<span class="sd">    - Similar for object::</span>

<span class="sd">        Union[int, object] == object</span>

<span class="sd">    - You cannot subclass or instantiate a union.</span>

<span class="sd">    - You can use Optional[X] as a shorthand for Union[X, None].</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__parameters__&#39;</span><span class="p">,</span> <span class="s1">&#39;__args__&#39;</span><span class="p">,</span> <span class="s1">&#39;__origin__&#39;</span><span class="p">,</span> <span class="s1">&#39;__tree_hash__&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">origin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">parameters</span><span class="p">,</span> <span class="n">origin</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="n">_root</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">origin</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__parameters__</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span> <span class="o">=</span> <span class="nb">hash</span><span class="p">(</span><span class="nb">frozenset</span><span class="p">((</span><span class="s1">&#39;Union&#39;</span><span class="p">,)))</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Expected parameters=&lt;tuple&gt;&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">origin</span> <span class="ow">is</span> <span class="n">Union</span><span class="p">:</span>
            <span class="n">parameters</span> <span class="o">=</span> <span class="n">_remove_dups_flatten</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>
            <span class="c1"># It&#39;s not a union if there&#39;s only one type left.</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">parameters</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__parameters__</span> <span class="o">=</span> <span class="n">_type_vars</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="n">parameters</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="o">=</span> <span class="n">origin</span>
        <span class="c1"># Pre-calculate the __hash__ on instantiation.</span>
        <span class="c1"># This improves speed for complex substitutions.</span>
        <span class="n">subs_tree</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subs_tree</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">subs_tree</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span> <span class="o">=</span> <span class="nb">hash</span><span class="p">(</span><span class="nb">frozenset</span><span class="p">(</span><span class="n">subs_tree</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span> <span class="o">=</span> <span class="nb">hash</span><span class="p">(</span><span class="n">subs_tree</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="n">ev_args</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">_eval_type</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span><span class="p">)</span>
        <span class="n">ev_origin</span> <span class="o">=</span> <span class="n">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ev_args</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="ow">and</span> <span class="n">ev_origin</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span><span class="p">:</span>
            <span class="c1"># Everything is already evaluated.</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="n">ev_args</span><span class="p">,</span> <span class="n">ev_origin</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_type_vars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">__parameters__</span><span class="p">:</span>
            <span class="n">_get_type_vars</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__parameters__</span><span class="p">,</span> <span class="n">tvars</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span>
        <span class="n">tree</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subs_tree</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="n">tree</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">tree</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">_tree_repr</span><span class="p">(</span><span class="n">tree</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_tree_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="n">arg_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">tree</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                <span class="n">arg_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_type_repr</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">arg_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">_tree_repr</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;[</span><span class="si">%s</span><span class="s1">]&#39;</span> <span class="o">%</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">arg_list</span><span class="p">)</span>

    <span class="nd">@_tp_cache</span>
    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">parameters</span> <span class="o">==</span> <span class="p">():</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot take a Union of no types.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="n">parameters</span> <span class="o">=</span> <span class="p">(</span><span class="n">parameters</span><span class="p">,)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Union[arg, ...]: each arg must be a type.&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Parameters to generic types must be types.&quot;</span>
        <span class="n">parameters</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">_type_check</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parameters</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Union</span><span class="p">:</span>
            <span class="n">_check_generic</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parameters</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="n">origin</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_subs_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">Union</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">Union</span>  <span class="c1"># Nothing to substitute</span>
        <span class="n">tree_args</span> <span class="o">=</span> <span class="n">_subs_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
        <span class="n">tree_args</span> <span class="o">=</span> <span class="n">_remove_dups_flatten</span><span class="p">(</span><span class="n">tree_args</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tree_args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">tree_args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>  <span class="c1"># Union of a single type is that type</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">Union</span><span class="p">,)</span> <span class="o">+</span> <span class="n">tree_args</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">_Union</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">__tree_hash__</span>
        <span class="k">elif</span> <span class="bp">self</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Union</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subs_tree</span><span class="p">()</span> <span class="o">==</span> <span class="n">other</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">other</span>

    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Unions cannot be used with isinstance().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Unions cannot be used with issubclass().&quot;</span><span class="p">)</span>


<span class="n">Union</span> <span class="o">=</span> <span class="n">_Union</span><span class="p">(</span><span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_Optional</span><span class="p">(</span><span class="n">_FinalTypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Optional type.</span>

<span class="sd">    Optional[X] is equivalent to Union[X, None].</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@_tp_cache</span>
    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
        <span class="n">arg</span> <span class="o">=</span> <span class="n">_type_check</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="s2">&quot;Optional[t] requires a single type.&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">Union</span><span class="p">[</span><span class="n">arg</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">)]</span>


<span class="n">Optional</span> <span class="o">=</span> <span class="n">_Optional</span><span class="p">(</span><span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_next_in_mro</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Helper for Generic.__new__.</span>

<span class="sd">    Returns the class after the last occurrence of Generic or</span>
<span class="sd">    Generic[...] in cls.__mro__.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">next_in_mro</span> <span class="o">=</span> <span class="nb">object</span>
    <span class="c1"># Look for the last occurrence of Generic or Generic[...].</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">GenericMeta</span><span class="p">)</span> <span class="ow">and</span> <span class="n">c</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Generic</span><span class="p">:</span>
            <span class="n">next_in_mro</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">next_in_mro</span>


<span class="k">def</span> <span class="nf">_make_subclasshook</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Construct a __subclasshook__ callable that incorporates</span>
<span class="sd">    the associated __extra__ class in subclass checks performed</span>
<span class="sd">    against cls.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">__extra__</span><span class="p">,</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
        <span class="c1"># The logic mirrors that of ABCMeta.__subclasscheck__.</span>
        <span class="c1"># Registered classes need not be checked here because</span>
        <span class="c1"># cls and its extra share the same _abc_registry.</span>
        <span class="k">def</span> <span class="nf">__extrahook__</span><span class="p">(</span><span class="n">subclass</span><span class="p">):</span>
            <span class="n">res</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__extra__</span><span class="o">.</span><span class="n">__subclasshook__</span><span class="p">(</span><span class="n">subclass</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">res</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">NotImplemented</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">res</span>
            <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__extra__</span> <span class="ow">in</span> <span class="n">subclass</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
            <span class="k">for</span> <span class="n">scls</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__extra__</span><span class="o">.</span><span class="n">__subclasses__</span><span class="p">():</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scls</span><span class="p">,</span> <span class="n">GenericMeta</span><span class="p">):</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">subclass</span><span class="p">,</span> <span class="n">scls</span><span class="p">):</span>
                    <span class="k">return</span> <span class="kc">True</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># For non-ABC extras we&#39;ll just call issubclass().</span>
        <span class="k">def</span> <span class="nf">__extrahook__</span><span class="p">(</span><span class="n">subclass</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__extra__</span> <span class="ow">and</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">subclass</span><span class="p">,</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__extra__</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">True</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
    <span class="k">return</span> <span class="n">__extrahook__</span>


<span class="k">def</span> <span class="nf">_no_slots_copy</span><span class="p">(</span><span class="n">dct</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal helper: copy class __dict__ and clean slots class variables.</span>
<span class="sd">    (They will be re-created if necessary by normal class machinery.)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">dict_copy</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">dct</span><span class="p">)</span>
    <span class="k">if</span> <span class="s1">&#39;__slots__&#39;</span> <span class="ow">in</span> <span class="n">dict_copy</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">slot</span> <span class="ow">in</span> <span class="n">dict_copy</span><span class="p">[</span><span class="s1">&#39;__slots__&#39;</span><span class="p">]:</span>
            <span class="n">dict_copy</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">slot</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">dict_copy</span>


<span class="k">class</span> <span class="nc">GenericMeta</span><span class="p">(</span><span class="n">TypingMeta</span><span class="p">,</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Metaclass for generic types.</span>

<span class="sd">    This is a metaclass for typing.Generic and generic ABCs defined in</span>
<span class="sd">    typing module. User defined subclasses of GenericMeta can override</span>
<span class="sd">    __new__ and invoke super().__new__. Note that GenericMeta.__new__</span>
<span class="sd">    has strict rules on what is allowed in its bases argument:</span>
<span class="sd">    * plain Generic is disallowed in bases;</span>
<span class="sd">    * Generic[...] should appear in bases at most once;</span>
<span class="sd">    * if Generic[...] is present, then it should list all type variables</span>
<span class="sd">      that appear in other bases.</span>
<span class="sd">    In addition, type of all generic bases is erased, e.g., C[int] is</span>
<span class="sd">    stripped to plain C.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span>
                <span class="n">tvars</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">origin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">orig_bases</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new generic class. GenericMeta.__new__ accepts</span>
<span class="sd">        keyword arguments that are used for internal bookkeeping, therefore</span>
<span class="sd">        an override should pass unused keyword arguments to super().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">tvars</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Called from __getitem__() below.</span>
            <span class="k">assert</span> <span class="n">origin</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
            <span class="k">assert</span> <span class="nb">all</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">TypeVar</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">tvars</span><span class="p">),</span> <span class="n">tvars</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Called from class statement.</span>
            <span class="k">assert</span> <span class="n">tvars</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">,</span> <span class="n">tvars</span>
            <span class="k">assert</span> <span class="n">args</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">,</span> <span class="n">args</span>
            <span class="k">assert</span> <span class="n">origin</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">,</span> <span class="n">origin</span>

            <span class="c1"># Get the full set of tvars from the bases.</span>
            <span class="n">tvars</span> <span class="o">=</span> <span class="n">_type_vars</span><span class="p">(</span><span class="n">bases</span><span class="p">)</span>
            <span class="c1"># Look for Generic[T1, ..., Tn].</span>
            <span class="c1"># If found, tvars must be a subset of it.</span>
            <span class="c1"># If not found, tvars is it.</span>
            <span class="c1"># Also check for and reject plain Generic,</span>
            <span class="c1"># and reject multiple Generic[...].</span>
            <span class="n">gvars</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="n">bases</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">base</span> <span class="ow">is</span> <span class="n">Generic</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot inherit from plain Generic&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="n">GenericMeta</span><span class="p">)</span> <span class="ow">and</span>
                        <span class="n">base</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="n">Generic</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">gvars</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                            <span class="s2">&quot;Cannot inherit from Generic[...] multiple types.&quot;</span><span class="p">)</span>
                    <span class="n">gvars</span> <span class="o">=</span> <span class="n">base</span><span class="o">.</span><span class="n">__parameters__</span>
            <span class="k">if</span> <span class="n">gvars</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">gvars</span> <span class="o">=</span> <span class="n">tvars</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">tvarset</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">tvars</span><span class="p">)</span>
                <span class="n">gvarset</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">gvars</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">tvarset</span> <span class="o">&lt;=</span> <span class="n">gvarset</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                        <span class="s2">&quot;Some type variables (</span><span class="si">%s</span><span class="s2">) &quot;</span>
                        <span class="s2">&quot;are not listed in Generic[</span><span class="si">%s</span><span class="s2">]&quot;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">tvars</span> <span class="k">if</span> <span class="n">t</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">gvarset</span><span class="p">),</span>
                         <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">g</span><span class="p">)</span> <span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">gvars</span><span class="p">)))</span>
                <span class="n">tvars</span> <span class="o">=</span> <span class="n">gvars</span>

        <span class="n">initial_bases</span> <span class="o">=</span> <span class="n">bases</span>
        <span class="k">if</span> <span class="n">extra</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">type</span><span class="p">(</span><span class="n">extra</span><span class="p">)</span> <span class="ow">is</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span> <span class="ow">and</span> <span class="n">extra</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">bases</span><span class="p">:</span>
            <span class="n">bases</span> <span class="o">=</span> <span class="p">(</span><span class="n">extra</span><span class="p">,)</span> <span class="o">+</span> <span class="n">bases</span>
        <span class="n">bases</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">_gorg</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">GenericMeta</span><span class="p">)</span> <span class="k">else</span> <span class="n">b</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">bases</span><span class="p">)</span>

        <span class="c1"># remove bare Generic from bases if there are other generic bases</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">GenericMeta</span><span class="p">)</span> <span class="ow">and</span> <span class="n">b</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Generic</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">bases</span><span class="p">):</span>
            <span class="n">bases</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">b</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">bases</span> <span class="k">if</span> <span class="n">b</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Generic</span><span class="p">)</span>
        <span class="n">namespace</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;__origin__&#39;</span><span class="p">:</span> <span class="n">origin</span><span class="p">,</span> <span class="s1">&#39;__extra__&#39;</span><span class="p">:</span> <span class="n">extra</span><span class="p">,</span>
                          <span class="s1">&#39;_gorg&#39;</span><span class="p">:</span> <span class="kc">None</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">origin</span> <span class="k">else</span> <span class="n">origin</span><span class="o">.</span><span class="n">_gorg</span><span class="p">})</span>
        <span class="bp">self</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">GenericMeta</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__setattr__</span><span class="p">(</span><span class="s1">&#39;_gorg&#39;</span><span class="p">,</span>
                                             <span class="bp">self</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">origin</span> <span class="k">else</span> <span class="n">origin</span><span class="o">.</span><span class="n">_gorg</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__parameters__</span> <span class="o">=</span> <span class="n">tvars</span>
        <span class="c1"># Be prepared that GenericMeta will be subclassed by TupleMeta</span>
        <span class="c1"># and CallableMeta, those two allow ..., (), or [] in __args___.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="o">...</span> <span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="n">_TypingEllipsis</span> <span class="k">else</span>
                              <span class="p">()</span> <span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="n">_TypingEmpty</span> <span class="k">else</span>
                              <span class="n">a</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">args</span><span class="p">)</span> <span class="k">if</span> <span class="n">args</span> <span class="k">else</span> <span class="kc">None</span>
        <span class="c1"># Speed hack (https://github.com/python/typing/issues/196).</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__next_in_mro__</span> <span class="o">=</span> <span class="n">_next_in_mro</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="c1"># Preserve base classes on subclassing (__bases__ are type erased now).</span>
        <span class="k">if</span> <span class="n">orig_bases</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__orig_bases__</span> <span class="o">=</span> <span class="n">initial_bases</span>

        <span class="c1"># This allows unparameterized generic collections to be used</span>
        <span class="c1"># with issubclass() and isinstance() in the same way as their</span>
        <span class="c1"># collections.abc counterparts (e.g., isinstance([], Iterable)).</span>
        <span class="k">if</span> <span class="p">(</span>
            <span class="s1">&#39;__subclasshook__&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">namespace</span> <span class="ow">and</span> <span class="n">extra</span> <span class="ow">or</span>
            <span class="c1"># allow overriding</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__subclasshook__</span><span class="p">,</span> <span class="s1">&#39;__name__&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;__extrahook__&#39;</span>
        <span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__subclasshook__</span> <span class="o">=</span> <span class="n">_make_subclasshook</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">extra</span><span class="p">,</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_abc_registry</span> <span class="o">=</span> <span class="n">extra</span><span class="o">.</span><span class="n">_abc_registry</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_abc_cache</span> <span class="o">=</span> <span class="n">extra</span><span class="o">.</span><span class="n">_abc_cache</span>
        <span class="k">elif</span> <span class="n">origin</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_abc_registry</span> <span class="o">=</span> <span class="n">origin</span><span class="o">.</span><span class="n">_abc_registry</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_abc_cache</span> <span class="o">=</span> <span class="n">origin</span><span class="o">.</span><span class="n">_abc_cache</span>

        <span class="k">if</span> <span class="n">origin</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">origin</span><span class="p">,</span> <span class="s1">&#39;__qualname__&#39;</span><span class="p">):</span>  <span class="c1"># Fix for Python 3.2.</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__qualname__</span> <span class="o">=</span> <span class="n">origin</span><span class="o">.</span><span class="vm">__qualname__</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span> <span class="o">=</span> <span class="p">(</span><span class="nb">hash</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_subs_tree</span><span class="p">())</span> <span class="k">if</span> <span class="n">origin</span> <span class="k">else</span>
                              <span class="nb">super</span><span class="p">(</span><span class="n">GenericMeta</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__hash__</span><span class="p">())</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="c1"># _abc_negative_cache and _abc_negative_cache_version</span>
    <span class="c1"># realised as descriptors, since GenClass[t1, t2, ...] always</span>
    <span class="c1"># share subclass info with GenClass.</span>
    <span class="c1"># This is an important memory optimization.</span>
    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_abc_negative_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="p">,</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="o">.</span><span class="n">_abc_negative_cache</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span><span class="o">.</span><span class="n">_abc_generic_negative_cache</span>

    <span class="nd">@_abc_negative_cache</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">_abc_negative_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="p">,</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="o">.</span><span class="n">_abc_negative_cache</span> <span class="o">=</span> <span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_abc_generic_negative_cache</span> <span class="o">=</span> <span class="n">value</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_abc_negative_cache_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="p">,</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="o">.</span><span class="n">_abc_negative_cache_version</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span><span class="o">.</span><span class="n">_abc_generic_negative_cache_version</span>

    <span class="nd">@_abc_negative_cache_version</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">_abc_negative_cache_version</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="p">,</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="o">.</span><span class="n">_abc_negative_cache_version</span> <span class="o">=</span> <span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_abc_generic_negative_cache_version</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">_get_type_vars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">__parameters__</span><span class="p">:</span>
            <span class="n">_get_type_vars</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__parameters__</span><span class="p">,</span> <span class="n">tvars</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">):</span>
        <span class="n">ev_origin</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span><span class="o">.</span><span class="n">_eval_type</span><span class="p">(</span><span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span>
                     <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="k">else</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">ev_args</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">_eval_type</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span>
                        <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="k">else</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">ev_origin</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">and</span> <span class="n">ev_args</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                              <span class="bp">self</span><span class="o">.</span><span class="vm">__bases__</span><span class="p">,</span>
                              <span class="n">_no_slots_copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">),</span>
                              <span class="n">tvars</span><span class="o">=</span><span class="n">_type_vars</span><span class="p">(</span><span class="n">ev_args</span><span class="p">)</span> <span class="k">if</span> <span class="n">ev_args</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
                              <span class="n">args</span><span class="o">=</span><span class="n">ev_args</span><span class="p">,</span>
                              <span class="n">origin</span><span class="o">=</span><span class="n">ev_origin</span><span class="p">,</span>
                              <span class="n">extra</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="p">,</span>
                              <span class="n">orig_bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">__orig_bases__</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tree_repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_subs_tree</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">_tree_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="n">arg_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">tree</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
            <span class="k">if</span> <span class="n">arg</span> <span class="o">==</span> <span class="p">():</span>
                <span class="n">arg_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;()&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                <span class="n">arg_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_type_repr</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">arg_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">_tree_repr</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;[</span><span class="si">%s</span><span class="s1">]&#39;</span> <span class="o">%</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">arg_list</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_subs_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="n">tree_args</span> <span class="o">=</span> <span class="n">_subs_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tvars</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span><span class="p">,)</span> <span class="o">+</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">tree_args</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">GenericMeta</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">other</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">__tree_hash__</span>

    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tree_hash__</span>

    <span class="nd">@_tp_cache</span>
    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="n">params</span> <span class="o">=</span> <span class="p">(</span><span class="n">params</span><span class="p">,)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">params</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Tuple</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                <span class="s2">&quot;Parameter list to </span><span class="si">%s</span><span class="s2">[...] cannot be empty&quot;</span> <span class="o">%</span> <span class="n">_qualname</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Parameters to generic types must be types.&quot;</span>
        <span class="n">params</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">_type_check</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">params</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">Generic</span><span class="p">:</span>
            <span class="c1"># Generic can only be subscripted with unique type variables.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">TypeVar</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">params</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                    <span class="s2">&quot;Parameters to Generic[...] must all be type variables&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">params</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">params</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                    <span class="s2">&quot;Parameters to Generic[...] must all be unique&quot;</span><span class="p">)</span>
            <span class="n">tvars</span> <span class="o">=</span> <span class="n">params</span>
            <span class="n">args</span> <span class="o">=</span> <span class="n">params</span>
        <span class="k">elif</span> <span class="bp">self</span> <span class="ow">in</span> <span class="p">(</span><span class="n">Tuple</span><span class="p">,</span> <span class="n">Callable</span><span class="p">):</span>
            <span class="n">tvars</span> <span class="o">=</span> <span class="n">_type_vars</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
            <span class="n">args</span> <span class="o">=</span> <span class="n">params</span>
        <span class="k">elif</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">_Protocol</span><span class="p">:</span>
            <span class="c1"># _Protocol is internal, don&#39;t check anything.</span>
            <span class="n">tvars</span> <span class="o">=</span> <span class="n">params</span>
            <span class="n">args</span> <span class="o">=</span> <span class="n">params</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">in</span> <span class="p">(</span><span class="n">Generic</span><span class="p">,</span> <span class="n">_Protocol</span><span class="p">):</span>
            <span class="c1"># Can&#39;t subscript Generic[...] or _Protocol[...].</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot subscript already-subscripted </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
                            <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Subscripting a regular Generic subclass.</span>
            <span class="n">_check_generic</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
            <span class="n">tvars</span> <span class="o">=</span> <span class="n">_type_vars</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
            <span class="n">args</span> <span class="o">=</span> <span class="n">params</span>

        <span class="n">prepend</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="p">,)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                              <span class="n">prepend</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__bases__</span><span class="p">,</span>
                              <span class="n">_no_slots_copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">),</span>
                              <span class="n">tvars</span><span class="o">=</span><span class="n">tvars</span><span class="p">,</span>
                              <span class="n">args</span><span class="o">=</span><span class="n">args</span><span class="p">,</span>
                              <span class="n">origin</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                              <span class="n">extra</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">__extra__</span><span class="p">,</span>
                              <span class="n">orig_bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">__orig_bases__</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">_getframe</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">f_globals</span><span class="p">[</span><span class="s1">&#39;__name__&#39;</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;abc&#39;</span><span class="p">,</span> <span class="s1">&#39;functools&#39;</span><span class="p">]:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Parameterized generics cannot be used with class &quot;</span>
                                <span class="s2">&quot;or instance checks&quot;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">Generic</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Class </span><span class="si">%r</span><span class="s2"> cannot be used with class &quot;</span>
                            <span class="s2">&quot;or instance checks&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__subclasscheck__</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="c1"># Since we extend ABC.__subclasscheck__ and</span>
        <span class="c1"># ABC.__instancecheck__ inlines the cache checking done by the</span>
        <span class="c1"># latter, we must extend __instancecheck__ too. For simplicity</span>
        <span class="c1"># we just skip the cache check -- instance checks for generic</span>
        <span class="c1"># classes are supposed to be rare anyways.</span>
        <span class="k">return</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># We consider all the subscripted generics as proxies for original class</span>
        <span class="k">if</span> <span class="p">(</span>
            <span class="n">attr</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;__&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">attr</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;__&#39;</span><span class="p">)</span> <span class="ow">or</span>
            <span class="n">attr</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_abc_&#39;</span><span class="p">)</span> <span class="ow">or</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="kc">None</span>  <span class="c1"># The class is not fully created, see #typing/506</span>
        <span class="p">):</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">GenericMeta</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__setattr__</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">GenericMeta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span><span class="p">)</span><span class="o">.</span><span class="fm">__setattr__</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>


<span class="c1"># Prevent checks for Generic to crash when defining Generic.</span>
<span class="n">Generic</span> <span class="o">=</span> <span class="kc">None</span>


<span class="k">def</span> <span class="nf">_generic_new</span><span class="p">(</span><span class="n">base_cls</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
    <span class="c1"># Assure type is erased on instantiation,</span>
    <span class="c1"># but attempt to store it in __orig_class__</span>
    <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">base_cls</span><span class="o">.</span><span class="fm">__new__</span> <span class="ow">is</span> <span class="nb">object</span><span class="o">.</span><span class="fm">__new__</span> <span class="ow">and</span>
                <span class="bp">cls</span><span class="o">.</span><span class="fm">__init__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">object</span><span class="o">.</span><span class="fm">__init__</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">base_cls</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">base_cls</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">origin</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">base_cls</span><span class="o">.</span><span class="fm">__new__</span> <span class="ow">is</span> <span class="nb">object</span><span class="o">.</span><span class="fm">__new__</span> <span class="ow">and</span>
                <span class="bp">cls</span><span class="o">.</span><span class="fm">__init__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">object</span><span class="o">.</span><span class="fm">__init__</span><span class="p">):</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="n">base_cls</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">origin</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="n">base_cls</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">origin</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">__orig_class__</span> <span class="o">=</span> <span class="bp">cls</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="n">obj</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>


<span class="k">class</span> <span class="nc">Generic</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">GenericMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Abstract base class for generic types.</span>

<span class="sd">    A generic type is typically declared by inheriting from</span>
<span class="sd">    this class parameterized with one or more type variables.</span>
<span class="sd">    For example, a generic mapping type might be defined as::</span>

<span class="sd">      class Mapping(Generic[KT, VT]):</span>
<span class="sd">          def __getitem__(self, key: KT) -&gt; VT:</span>
<span class="sd">              ...</span>
<span class="sd">          # Etc.</span>

<span class="sd">    This class can then be used as follows::</span>

<span class="sd">      def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -&gt; VT:</span>
<span class="sd">          try:</span>
<span class="sd">              return mapping[key]</span>
<span class="sd">          except KeyError:</span>
<span class="sd">              return default</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Generic</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type Generic cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;it can be used only as a base class&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">__next_in_mro__</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_TypingEmpty</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Internal placeholder for () or []. Used by TupleMeta and CallableMeta</span>
<span class="sd">    to allow empty list/tuple in specific places, without allowing them</span>
<span class="sd">    to sneak in where prohibited.</span>
<span class="sd">    &quot;&quot;&quot;</span>


<span class="k">class</span> <span class="nc">_TypingEllipsis</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Internal placeholder for ... (ellipsis).&quot;&quot;&quot;</span>


<span class="k">class</span> <span class="nc">TupleMeta</span><span class="p">(</span><span class="n">GenericMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Metaclass for Tuple (internal).&quot;&quot;&quot;</span>

    <span class="nd">@_tp_cache</span>
    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Tuple</span><span class="p">:</span>
            <span class="c1"># Normal generic rules apply if this is not the first subscription</span>
            <span class="c1"># or a subscription of a subclass.</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">parameters</span> <span class="o">==</span> <span class="p">():</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">((</span><span class="n">_TypingEmpty</span><span class="p">,))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
            <span class="n">parameters</span> <span class="o">=</span> <span class="p">(</span><span class="n">parameters</span><span class="p">,)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">and</span> <span class="n">parameters</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">is</span> <span class="o">...</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Tuple[t, ...]: t must be a type.&quot;</span>
            <span class="n">p</span> <span class="o">=</span> <span class="n">_type_check</span><span class="p">(</span><span class="n">parameters</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">msg</span><span class="p">)</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">((</span><span class="n">p</span><span class="p">,</span> <span class="n">_TypingEllipsis</span><span class="p">))</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Tuple[t0, t1, ...]: each t must be a type.&quot;</span>
        <span class="n">parameters</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">_type_check</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parameters</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Parameterized Tuple cannot be used &quot;</span>
                        <span class="s2">&quot;with isinstance().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__args__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">issubclass</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Parameterized Tuple cannot be used &quot;</span>
                        <span class="s2">&quot;with issubclass().&quot;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Tuple</span><span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">metaclass</span><span class="o">=</span><span class="n">TupleMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Tuple type; Tuple[X, Y] is the cross-product type of X and Y.</span>

<span class="sd">    Example: Tuple[T1, T2] is a tuple of two elements corresponding</span>
<span class="sd">    to type variables T1 and T2.  Tuple[int, float, str] is a tuple</span>
<span class="sd">    of an int, a float and a string.</span>

<span class="sd">    To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Tuple</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type Tuple cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;use tuple() instead&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">CallableMeta</span><span class="p">(</span><span class="n">GenericMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Metaclass for Callable (internal).&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tree_repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_subs_tree</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">_tree_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Callable</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">_tree_repr</span><span class="p">(</span><span class="n">tree</span><span class="p">)</span>
        <span class="c1"># For actual Callable (not its subclass) we override</span>
        <span class="c1"># super()._tree_repr() for nice formatting.</span>
        <span class="n">arg_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">tree</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                <span class="n">arg_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_type_repr</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">arg_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">_tree_repr</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">arg_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;...&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;[..., </span><span class="si">%s</span><span class="s1">]&#39;</span> <span class="o">%</span> <span class="n">arg_list</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">tree</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span>
                <span class="s1">&#39;[[</span><span class="si">%s</span><span class="s1">], </span><span class="si">%s</span><span class="s1">]&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">arg_list</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">arg_list</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;A thin wrapper around __getitem_inner__ to provide the latter</span>
<span class="sd">        with hashable arguments to improve speed.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__origin__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">Callable</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Callable must be used as &quot;</span>
                            <span class="s2">&quot;Callable[[arg, ...], result].&quot;</span><span class="p">)</span>
        <span class="n">args</span><span class="p">,</span> <span class="n">result</span> <span class="o">=</span> <span class="n">parameters</span>
        <span class="k">if</span> <span class="n">args</span> <span class="ow">is</span> <span class="bp">Ellipsis</span><span class="p">:</span>
            <span class="n">parameters</span> <span class="o">=</span> <span class="p">(</span><span class="bp">Ellipsis</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Callable[args, result]: args must be a list.&quot;</span>
                                <span class="s2">&quot; Got </span><span class="si">%.100r</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">args</span><span class="p">,))</span>
            <span class="n">parameters</span> <span class="o">=</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">args</span><span class="p">),</span> <span class="n">result</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getitem_inner__</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>

    <span class="nd">@_tp_cache</span>
    <span class="k">def</span> <span class="nf">__getitem_inner__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
        <span class="n">args</span><span class="p">,</span> <span class="n">result</span> <span class="o">=</span> <span class="n">parameters</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Callable[args, result]: result must be a type.&quot;</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">_type_check</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">args</span> <span class="ow">is</span> <span class="bp">Ellipsis</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">((</span><span class="n">_TypingEllipsis</span><span class="p">,</span> <span class="n">result</span><span class="p">))</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Callable[[arg, ...], result]: each arg must be a type.&quot;</span>
        <span class="n">args</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">_type_check</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">)</span>
        <span class="n">parameters</span> <span class="o">=</span> <span class="n">args</span> <span class="o">+</span> <span class="p">(</span><span class="n">result</span><span class="p">,)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Callable</span><span class="p">(</span><span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Callable</span><span class="p">,</span> <span class="n">metaclass</span><span class="o">=</span><span class="n">CallableMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Callable type; Callable[[int], str] is a function of (int) -&gt; str.</span>

<span class="sd">    The subscription syntax must always be used with exactly two</span>
<span class="sd">    values: the argument list and the return type.  The argument list</span>
<span class="sd">    must be a list of types or ellipsis; the return type must be a single type.</span>

<span class="sd">    There is no syntax to indicate optional or keyword arguments,</span>
<span class="sd">    such function types are rarely used as callback types.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Callable</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type Callable cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;use a non-abstract subclass instead&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">__next_in_mro__</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_ClassVar</span><span class="p">(</span><span class="n">_FinalTypingBase</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Special type construct to mark class variables.</span>

<span class="sd">    An annotation wrapped in ClassVar indicates that a given</span>
<span class="sd">    attribute is intended to be used as a class variable and</span>
<span class="sd">    should not be set on instances of that class. Usage::</span>

<span class="sd">      class Starship:</span>
<span class="sd">          stats: ClassVar[Dict[str, int]] = {} # class variable</span>
<span class="sd">          damage: int = 10                     # instance variable</span>

<span class="sd">    ClassVar accepts only types and cannot be further subscribed.</span>

<span class="sd">    Note that ClassVar is not a class itself, and should not</span>
<span class="sd">    be used with isinstance() or issubclass().</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__type__&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tp</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__type__</span> <span class="o">=</span> <span class="n">tp</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">cls</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__type__</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">_type_check</span><span class="p">(</span><span class="n">item</span><span class="p">,</span>
                       <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> accepts only single type.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">[</span><span class="mi">1</span><span class="p">:])),</span>
                       <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> cannot be further subscripted&#39;</span>
                        <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span>

    <span class="k">def</span> <span class="nf">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">):</span>
        <span class="n">new_tp</span> <span class="o">=</span> <span class="n">_eval_type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__type__</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">new_tp</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__type__</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)(</span><span class="n">new_tp</span><span class="p">,</span> <span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">r</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__type__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">r</span> <span class="o">+=</span> <span class="s1">&#39;[</span><span class="si">{}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">_type_repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__type__</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">r</span>

    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hash</span><span class="p">((</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__type__</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">_ClassVar</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__type__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__type__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">__type__</span>
        <span class="k">return</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">other</span>


<span class="n">ClassVar</span> <span class="o">=</span> <span class="n">_ClassVar</span><span class="p">(</span><span class="n">_root</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">cast</span><span class="p">(</span><span class="n">typ</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Cast a value to a type.</span>

<span class="sd">    This returns the value unchanged.  To the type checker this</span>
<span class="sd">    signals that the return value has the designated type, but at</span>
<span class="sd">    runtime we intentionally don&#39;t check anything (we want this</span>
<span class="sd">    to be as fast as possible).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">val</span>


<span class="k">def</span> <span class="nf">_get_defaults</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal helper to extract the default arguments, by name.&quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">code</span> <span class="o">=</span> <span class="n">func</span><span class="o">.</span><span class="vm">__code__</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="c1"># Some built-in functions don&#39;t have __code__, __defaults__, etc.</span>
        <span class="k">return</span> <span class="p">{}</span>
    <span class="n">pos_count</span> <span class="o">=</span> <span class="n">code</span><span class="o">.</span><span class="n">co_argcount</span>
    <span class="n">arg_names</span> <span class="o">=</span> <span class="n">code</span><span class="o">.</span><span class="n">co_varnames</span>
    <span class="n">arg_names</span> <span class="o">=</span> <span class="n">arg_names</span><span class="p">[:</span><span class="n">pos_count</span><span class="p">]</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="n">func</span><span class="o">.</span><span class="vm">__defaults__</span> <span class="ow">or</span> <span class="p">()</span>
    <span class="n">kwdefaults</span> <span class="o">=</span> <span class="n">func</span><span class="o">.</span><span class="vm">__kwdefaults__</span>
    <span class="n">res</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">kwdefaults</span><span class="p">)</span> <span class="k">if</span> <span class="n">kwdefaults</span> <span class="k">else</span> <span class="p">{}</span>
    <span class="n">pos_offset</span> <span class="o">=</span> <span class="n">pos_count</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">defaults</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">arg_names</span><span class="p">[</span><span class="n">pos_offset</span><span class="p">:],</span> <span class="n">defaults</span><span class="p">):</span>
        <span class="k">assert</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">res</span>
        <span class="n">res</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
    <span class="k">return</span> <span class="n">res</span>


<span class="n">_allowed_types</span> <span class="o">=</span> <span class="p">(</span><span class="n">types</span><span class="o">.</span><span class="n">FunctionType</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">BuiltinFunctionType</span><span class="p">,</span>
                  <span class="n">types</span><span class="o">.</span><span class="n">MethodType</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">ModuleType</span><span class="p">,</span>
                  <span class="n">WrapperDescriptorType</span><span class="p">,</span> <span class="n">MethodWrapperType</span><span class="p">,</span> <span class="n">MethodDescriptorType</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">get_type_hints</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">globalns</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">localns</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return type hints for an object.</span>

<span class="sd">    This is often the same as obj.__annotations__, but it handles</span>
<span class="sd">    forward references encoded as string literals, and if necessary</span>
<span class="sd">    adds Optional[t] if a default value equal to None is set.</span>

<span class="sd">    The argument may be a module, class, method, or function. The annotations</span>
<span class="sd">    are returned as a dictionary. For classes, annotations include also</span>
<span class="sd">    inherited members.</span>

<span class="sd">    TypeError is raised if the argument is not of a type that can contain</span>
<span class="sd">    annotations, and an empty dictionary is returned if no annotations are</span>
<span class="sd">    present.</span>

<span class="sd">    BEWARE -- the behavior of globalns and localns is counterintuitive</span>
<span class="sd">    (unless you are familiar with how eval() and exec() work).  The</span>
<span class="sd">    search order is locals first, then globals.</span>

<span class="sd">    - If no dict arguments are passed, an attempt is made to use the</span>
<span class="sd">      globals from obj (or the respective module&#39;s globals for classes),</span>
<span class="sd">      and these are also used as the locals.  If the object does not appear</span>
<span class="sd">      to have globals, an empty dictionary is used.</span>

<span class="sd">    - If one dict argument is passed, it is used for both globals and</span>
<span class="sd">      locals.</span>

<span class="sd">    - If two dict arguments are passed, they specify globals and</span>
<span class="sd">      locals, respectively.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;__no_type_check__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{}</span>
    <span class="c1"># Classes require a special treatment.</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
        <span class="n">hints</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">globalns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">base_globals</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">base</span><span class="o">.</span><span class="vm">__module__</span><span class="p">]</span><span class="o">.</span><span class="vm">__dict__</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">base_globals</span> <span class="o">=</span> <span class="n">globalns</span>
            <span class="n">ann</span> <span class="o">=</span> <span class="n">base</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;__annotations__&#39;</span><span class="p">,</span> <span class="p">{})</span>
            <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">ann</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="n">_ForwardRef</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">_eval_type</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">base_globals</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span>
                <span class="n">hints</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">return</span> <span class="n">hints</span>

    <span class="k">if</span> <span class="n">globalns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">ModuleType</span><span class="p">):</span>
            <span class="n">globalns</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="vm">__dict__</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">globalns</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;__globals__&#39;</span><span class="p">,</span> <span class="p">{})</span>
        <span class="k">if</span> <span class="n">localns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">localns</span> <span class="o">=</span> <span class="n">globalns</span>
    <span class="k">elif</span> <span class="n">localns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">localns</span> <span class="o">=</span> <span class="n">globalns</span>
    <span class="n">hints</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;__annotations__&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">hints</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># Return empty annotations for something that _could_ have them.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">_allowed_types</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">{}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{!r}</span><span class="s1"> is not a module, class, method, &#39;</span>
                            <span class="s1">&#39;or function.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">obj</span><span class="p">))</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="n">_get_defaults</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
    <span class="n">hints</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">hints</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">hints</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">_ForwardRef</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">_eval_type</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">globalns</span><span class="p">,</span> <span class="n">localns</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">defaults</span> <span class="ow">and</span> <span class="n">defaults</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">Optional</span><span class="p">[</span><span class="n">value</span><span class="p">]</span>
        <span class="n">hints</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
    <span class="k">return</span> <span class="n">hints</span>


<span class="k">def</span> <span class="nf">no_type_check</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator to indicate that annotations are not type hints.</span>

<span class="sd">    The argument must be a class or function; if it is a class, it</span>
<span class="sd">    applies recursively to all methods and classes defined in that class</span>
<span class="sd">    (but not to methods defined in its superclasses or subclasses).</span>

<span class="sd">    This mutates the function(s) or class(es) in place.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
        <span class="n">arg_attrs</span> <span class="o">=</span> <span class="n">arg</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">attr</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">arg</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">arg</span><span class="o">.</span><span class="vm">__bases__</span> <span class="o">+</span> <span class="p">(</span><span class="n">arg</span><span class="p">,):</span>
                <span class="n">arg_attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">arg_attrs</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">FunctionType</span><span class="p">):</span>
                <span class="n">obj</span><span class="o">.</span><span class="n">__no_type_check__</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
                <span class="n">no_type_check</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">arg</span><span class="o">.</span><span class="n">__no_type_check__</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>  <span class="c1"># built-in classes</span>
        <span class="k">pass</span>
    <span class="k">return</span> <span class="n">arg</span>


<span class="k">def</span> <span class="nf">no_type_check_decorator</span><span class="p">(</span><span class="n">decorator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator to give another decorator the @no_type_check effect.</span>

<span class="sd">    This wraps the decorator with something that wraps the decorated</span>
<span class="sd">    function in @no_type_check.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@functools</span><span class="o">.</span><span class="n">wraps</span><span class="p">(</span><span class="n">decorator</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapped_decorator</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="n">func</span> <span class="o">=</span> <span class="n">decorator</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="n">func</span> <span class="o">=</span> <span class="n">no_type_check</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">func</span>

    <span class="k">return</span> <span class="n">wrapped_decorator</span>


<span class="k">def</span> <span class="nf">_overload_dummy</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Helper for @overload to raise when called.&quot;&quot;&quot;</span>
    <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
        <span class="s2">&quot;You should not call an overloaded function. &quot;</span>
        <span class="s2">&quot;A series of @overload-decorated functions &quot;</span>
        <span class="s2">&quot;outside a stub module should always be followed &quot;</span>
        <span class="s2">&quot;by an implementation that is not @overload-ed.&quot;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">overload</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator for overloaded functions/methods.</span>

<span class="sd">    In a stub file, place two or more stub definitions for the same</span>
<span class="sd">    function in a row, each decorated with @overload.  For example:</span>

<span class="sd">      @overload</span>
<span class="sd">      def utf8(value: None) -&gt; None: ...</span>
<span class="sd">      @overload</span>
<span class="sd">      def utf8(value: bytes) -&gt; bytes: ...</span>
<span class="sd">      @overload</span>
<span class="sd">      def utf8(value: str) -&gt; bytes: ...</span>

<span class="sd">    In a non-stub file (i.e. a regular .py file), do the same but</span>
<span class="sd">    follow it with an implementation.  The implementation should *not*</span>
<span class="sd">    be decorated with @overload.  For example:</span>

<span class="sd">      @overload</span>
<span class="sd">      def utf8(value: None) -&gt; None: ...</span>
<span class="sd">      @overload</span>
<span class="sd">      def utf8(value: bytes) -&gt; bytes: ...</span>
<span class="sd">      @overload</span>
<span class="sd">      def utf8(value: str) -&gt; bytes: ...</span>
<span class="sd">      def utf8(value):</span>
<span class="sd">          # implementation goes here</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_overload_dummy</span>


<span class="k">class</span> <span class="nc">_ProtocolMeta</span><span class="p">(</span><span class="n">GenericMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal metaclass for _Protocol.</span>

<span class="sd">    This exists so _Protocol classes can be generic without deriving</span>
<span class="sd">    from Generic.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">_Protocol</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__bases__</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__instancecheck__</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Protocols cannot be used with isinstance().&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__subclasscheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_protocol</span><span class="p">:</span>
            <span class="c1"># No structural checks since this isn&#39;t a protocol.</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>

        <span class="k">if</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">_Protocol</span><span class="p">:</span>
            <span class="c1"># Every class is a subclass of the empty protocol.</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="c1"># Find all attributes defined in the protocol.</span>
        <span class="n">attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_protocol_attrs</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">attrs</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="n">attr</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="vm">__dict__</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_get_protocol_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Get all Protocol base classes.</span>
        <span class="n">protocol_bases</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="s1">&#39;_is_protocol&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="ow">and</span> <span class="n">c</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s1">&#39;_Protocol&#39;</span><span class="p">:</span>
                <span class="n">protocol_bases</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>

        <span class="c1"># Get attributes included in protocol.</span>
        <span class="n">attrs</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="n">protocol_bases</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">base</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="c1"># Include attributes not defined in any non-protocol bases.</span>
                <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">:</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">c</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">base</span> <span class="ow">and</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">c</span><span class="o">.</span><span class="vm">__dict__</span> <span class="ow">and</span>
                            <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="s1">&#39;_is_protocol&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)):</span>
                        <span class="k">break</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">attr</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_abc_&#39;</span><span class="p">)</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__abstractmethods__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__annotations__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__weakref__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;_is_protocol&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;_gorg&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__dict__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__args__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__slots__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;_get_protocol_attrs&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__next_in_mro__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__parameters__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__origin__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__orig_bases__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__extra__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__tree_hash__&#39;</span> <span class="ow">and</span>
                            <span class="n">attr</span> <span class="o">!=</span> <span class="s1">&#39;__module__&#39;</span><span class="p">):</span>
                        <span class="n">attrs</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">attrs</span>


<span class="k">class</span> <span class="nc">_Protocol</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">_ProtocolMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Internal base class for protocol classes.</span>

<span class="sd">    This implements a simple-minded structural issubclass check</span>
<span class="sd">    (similar but more general than the one-offs in collections.abc</span>
<span class="sd">    such as Hashable).</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">_is_protocol</span> <span class="o">=</span> <span class="kc">True</span>


<span class="c1"># Various ABCs mimicking those in collections.abc.</span>
<span class="c1"># A few are simply re-exported for completeness.</span>

<span class="n">Hashable</span> <span class="o">=</span> <span class="n">collections_abc</span><span class="o">.</span><span class="n">Hashable</span>  <span class="c1"># Not generic.</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Awaitable&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Awaitable</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Awaitable</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;Awaitable&#39;</span><span class="p">)</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Coroutine&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Coroutine</span><span class="p">(</span><span class="n">Awaitable</span><span class="p">[</span><span class="n">V_co</span><span class="p">],</span> <span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">,</span> <span class="n">T_contra</span><span class="p">,</span> <span class="n">V_co</span><span class="p">],</span>
                    <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Coroutine</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;Coroutine&#39;</span><span class="p">)</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;AsyncIterable&#39;</span><span class="p">):</span>

    <span class="k">class</span> <span class="nc">AsyncIterable</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">AsyncIterable</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">class</span> <span class="nc">AsyncIterator</span><span class="p">(</span><span class="n">AsyncIterable</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                        <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">AsyncIterator</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;AsyncIterable&#39;</span><span class="p">)</span>
    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;AsyncIterator&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Iterable</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Iterable</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">Iterator</span><span class="p">(</span><span class="n">Iterable</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Iterator</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">SupportsInt</span><span class="p">(</span><span class="n">_Protocol</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__int__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">SupportsFloat</span><span class="p">(</span><span class="n">_Protocol</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__float__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">SupportsComplex</span><span class="p">(</span><span class="n">_Protocol</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__complex__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">complex</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">SupportsBytes</span><span class="p">(</span><span class="n">_Protocol</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__bytes__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bytes</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">SupportsAbs</span><span class="p">(</span><span class="n">_Protocol</span><span class="p">[</span><span class="n">T_co</span><span class="p">]):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__abs__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">T_co</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">SupportsRound</span><span class="p">(</span><span class="n">_Protocol</span><span class="p">[</span><span class="n">T_co</span><span class="p">]):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__round__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndigits</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">T_co</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Reversible&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Reversible</span><span class="p">(</span><span class="n">Iterable</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Reversible</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">Reversible</span><span class="p">(</span><span class="n">_Protocol</span><span class="p">[</span><span class="n">T_co</span><span class="p">]):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

        <span class="nd">@abstractmethod</span>
        <span class="k">def</span> <span class="nf">__reversed__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s1">&#39;Iterator[T_co]&#39;</span><span class="p">:</span>
            <span class="k">pass</span>


<span class="n">Sized</span> <span class="o">=</span> <span class="n">collections_abc</span><span class="o">.</span><span class="n">Sized</span>  <span class="c1"># Not generic.</span>


<span class="k">class</span> <span class="nc">Container</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Container</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Collection&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Collection</span><span class="p">(</span><span class="n">Sized</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">Container</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                     <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Collection</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;Collection&#39;</span><span class="p">)</span>


<span class="c1"># Callable was defined earlier.</span>

<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Collection&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">AbstractSet</span><span class="p">(</span><span class="n">Collection</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                      <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Set</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">AbstractSet</span><span class="p">(</span><span class="n">Sized</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">Container</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                      <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Set</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">MutableSet</span><span class="p">(</span><span class="n">AbstractSet</span><span class="p">[</span><span class="n">T</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">MutableSet</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="c1"># NOTE: It is only covariant in the value type.</span>
<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Collection&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Mapping</span><span class="p">(</span><span class="n">Collection</span><span class="p">[</span><span class="n">KT</span><span class="p">],</span> <span class="n">Generic</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT_co</span><span class="p">],</span>
                  <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Mapping</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">Mapping</span><span class="p">(</span><span class="n">Sized</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">KT</span><span class="p">],</span> <span class="n">Container</span><span class="p">[</span><span class="n">KT</span><span class="p">],</span> <span class="n">Generic</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT_co</span><span class="p">],</span>
                  <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Mapping</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">MutableMapping</span><span class="p">(</span><span class="n">Mapping</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">MutableMapping</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Reversible&#39;</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Collection&#39;</span><span class="p">):</span>
        <span class="k">class</span> <span class="nc">Sequence</span><span class="p">(</span><span class="n">Reversible</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">Collection</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                       <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Sequence</span><span class="p">):</span>
            <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">class</span> <span class="nc">Sequence</span><span class="p">(</span><span class="n">Sized</span><span class="p">,</span> <span class="n">Reversible</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">Container</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                       <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Sequence</span><span class="p">):</span>
            <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">Sequence</span><span class="p">(</span><span class="n">Sized</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">Container</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                   <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">Sequence</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">List</span><span class="p">(</span><span class="n">Sequence</span><span class="p">[</span><span class="n">T</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">List</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">ByteString</span><span class="p">(</span><span class="n">Sequence</span><span class="p">[</span><span class="nb">int</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">ByteString</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">List</span><span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="n">T</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="nb">list</span><span class="p">):</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">List</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type List cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;use list() instead&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Deque</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="n">T</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">):</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Deque</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Set</span><span class="p">(</span><span class="nb">set</span><span class="p">,</span> <span class="n">MutableSet</span><span class="p">[</span><span class="n">T</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="nb">set</span><span class="p">):</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Set</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type Set cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;use set() instead&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="nb">set</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">FrozenSet</span><span class="p">(</span><span class="nb">frozenset</span><span class="p">,</span> <span class="n">AbstractSet</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="nb">frozenset</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">FrozenSet</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type FrozenSet cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;use frozenset() instead&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="nb">frozenset</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">MappingView</span><span class="p">(</span><span class="n">Sized</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">MappingView</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">KeysView</span><span class="p">(</span><span class="n">MappingView</span><span class="p">[</span><span class="n">KT</span><span class="p">],</span> <span class="n">AbstractSet</span><span class="p">[</span><span class="n">KT</span><span class="p">],</span>
               <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">KeysView</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">ItemsView</span><span class="p">(</span><span class="n">MappingView</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT_co</span><span class="p">]],</span>
                <span class="n">AbstractSet</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT_co</span><span class="p">]],</span>
                <span class="n">Generic</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT_co</span><span class="p">],</span>
                <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">ItemsView</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">ValuesView</span><span class="p">(</span><span class="n">MappingView</span><span class="p">[</span><span class="n">VT_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">ValuesView</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">contextlib</span><span class="p">,</span> <span class="s1">&#39;AbstractContextManager&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">ContextManager</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">contextlib</span><span class="o">.</span><span class="n">AbstractContextManager</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">ContextManager</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">]):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

        <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span>

        <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
        <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="nd">@classmethod</span>
        <span class="k">def</span> <span class="nf">__subclasshook__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">C</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">cls</span> <span class="ow">is</span> <span class="n">ContextManager</span><span class="p">:</span>
                <span class="c1"># In Python 3.6+, it is possible to set a method to None to</span>
                <span class="c1"># explicitly indicate that the class does not implement an ABC</span>
                <span class="c1"># (https://bugs.python.org/issue25958), but we do not support</span>
                <span class="c1"># that pattern here because this fallback class is only used</span>
                <span class="c1"># in Python 3.5 and earlier.</span>
                <span class="k">if</span> <span class="p">(</span><span class="nb">any</span><span class="p">(</span><span class="s2">&quot;__enter__&quot;</span> <span class="ow">in</span> <span class="n">B</span><span class="o">.</span><span class="vm">__dict__</span> <span class="k">for</span> <span class="n">B</span> <span class="ow">in</span> <span class="n">C</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">)</span> <span class="ow">and</span>
                    <span class="nb">any</span><span class="p">(</span><span class="s2">&quot;__exit__&quot;</span> <span class="ow">in</span> <span class="n">B</span><span class="o">.</span><span class="vm">__dict__</span> <span class="k">for</span> <span class="n">B</span> <span class="ow">in</span> <span class="n">C</span><span class="o">.</span><span class="vm">__mro__</span><span class="p">)):</span>
                    <span class="k">return</span> <span class="kc">True</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">contextlib</span><span class="p">,</span> <span class="s1">&#39;AbstractAsyncContextManager&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">AsyncContextManager</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span>
                              <span class="n">extra</span><span class="o">=</span><span class="n">contextlib</span><span class="o">.</span><span class="n">AbstractAsyncContextManager</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;AsyncContextManager&#39;</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
    <span class="n">exec</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">class AsyncContextManager(Generic[T_co]):</span>
<span class="s2">    __slots__ = ()</span>

<span class="s2">    async def __aenter__(self):</span>
<span class="s2">        return self</span>

<span class="s2">    @abc.abstractmethod</span>
<span class="s2">    async def __aexit__(self, exc_type, exc_value, traceback):</span>
<span class="s2">        return None</span>

<span class="s2">    @classmethod</span>
<span class="s2">    def __subclasshook__(cls, C):</span>
<span class="s2">        if cls is AsyncContextManager:</span>
<span class="s2">            if sys.version_info[:2] &gt;= (3, 6):</span>
<span class="s2">                return _collections_abc._check_methods(C, &quot;__aenter__&quot;, &quot;__aexit__&quot;)</span>
<span class="s2">            if (any(&quot;__aenter__&quot; in B.__dict__ for B in C.__mro__) and</span>
<span class="s2">                    any(&quot;__aexit__&quot; in B.__dict__ for B in C.__mro__)):</span>
<span class="s2">                return True</span>
<span class="s2">        return NotImplemented</span>

<span class="s2">__all__.append(&#39;AsyncContextManager&#39;)</span>
<span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Dict</span><span class="p">(</span><span class="nb">dict</span><span class="p">,</span> <span class="n">MutableMapping</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="nb">dict</span><span class="p">):</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Dict</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type Dict cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;use dict() instead&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="nb">dict</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">DefaultDict</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">,</span> <span class="n">MutableMapping</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT</span><span class="p">],</span>
                  <span class="n">extra</span><span class="o">=</span><span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">):</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">DefaultDict</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Counter</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">Counter</span><span class="p">,</span> <span class="n">Dict</span><span class="p">[</span><span class="n">T</span><span class="p">,</span> <span class="nb">int</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="n">collections</span><span class="o">.</span><span class="n">Counter</span><span class="p">):</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Counter</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">collections</span><span class="o">.</span><span class="n">Counter</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">Counter</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections</span><span class="p">,</span> <span class="s1">&#39;ChainMap&#39;</span><span class="p">):</span>
    <span class="c1"># ChainMap only exists in 3.3+</span>
    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;ChainMap&#39;</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">ChainMap</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">ChainMap</span><span class="p">,</span> <span class="n">MutableMapping</span><span class="p">[</span><span class="n">KT</span><span class="p">,</span> <span class="n">VT</span><span class="p">],</span>
                   <span class="n">extra</span><span class="o">=</span><span class="n">collections</span><span class="o">.</span><span class="n">ChainMap</span><span class="p">):</span>

        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

        <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">ChainMap</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">collections</span><span class="o">.</span><span class="n">ChainMap</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">ChainMap</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="c1"># Determine what base class to use for Generator.</span>
<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;Generator&#39;</span><span class="p">):</span>
    <span class="c1"># Sufficiently recent versions of 3.5 have a Generator ABC.</span>
    <span class="n">_G_base</span> <span class="o">=</span> <span class="n">collections_abc</span><span class="o">.</span><span class="n">Generator</span>
<span class="k">else</span><span class="p">:</span>
    <span class="c1"># Fall back on the exact type.</span>
    <span class="n">_G_base</span> <span class="o">=</span> <span class="n">types</span><span class="o">.</span><span class="n">GeneratorType</span>


<span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">Iterator</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">,</span> <span class="n">T_contra</span><span class="p">,</span> <span class="n">V_co</span><span class="p">],</span>
                <span class="n">extra</span><span class="o">=</span><span class="n">_G_base</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_gorg</span> <span class="ow">is</span> <span class="n">Generator</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Type Generator cannot be instantiated; &quot;</span>
                            <span class="s2">&quot;create a subclass instead&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_generic_new</span><span class="p">(</span><span class="n">_G_base</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span>


<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">collections_abc</span><span class="p">,</span> <span class="s1">&#39;AsyncGenerator&#39;</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">AsyncGenerator</span><span class="p">(</span><span class="n">AsyncIterator</span><span class="p">[</span><span class="n">T_co</span><span class="p">],</span> <span class="n">Generic</span><span class="p">[</span><span class="n">T_co</span><span class="p">,</span> <span class="n">T_contra</span><span class="p">],</span>
                         <span class="n">extra</span><span class="o">=</span><span class="n">collections_abc</span><span class="o">.</span><span class="n">AsyncGenerator</span><span class="p">):</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;AsyncGenerator&#39;</span><span class="p">)</span>


<span class="c1"># Internal type variable used for Type[].</span>
<span class="n">CT_co</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;CT_co&#39;</span><span class="p">,</span> <span class="n">covariant</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">bound</span><span class="o">=</span><span class="nb">type</span><span class="p">)</span>


<span class="c1"># This is not a real generic class.  Don&#39;t use outside annotations.</span>
<span class="k">class</span> <span class="nc">Type</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">CT_co</span><span class="p">],</span> <span class="n">extra</span><span class="o">=</span><span class="nb">type</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A special construct usable to annotate class objects.</span>

<span class="sd">    For example, suppose we have the following classes::</span>

<span class="sd">      class User: ...  # Abstract base for User classes</span>
<span class="sd">      class BasicUser(User): ...</span>
<span class="sd">      class ProUser(User): ...</span>
<span class="sd">      class TeamUser(User): ...</span>

<span class="sd">    And a function that takes a class argument that&#39;s a subclass of</span>
<span class="sd">    User and returns an instance of the corresponding class::</span>

<span class="sd">      U = TypeVar(&#39;U&#39;, bound=User)</span>
<span class="sd">      def new_user(user_class: Type[U]) -&gt; U:</span>
<span class="sd">          user = user_class()</span>
<span class="sd">          # (Here we could write the user object to a database)</span>
<span class="sd">          return user</span>

<span class="sd">      joe = new_user(BasicUser)</span>

<span class="sd">    At this point the type checker knows that joe has type BasicUser.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">def</span> <span class="nf">_make_nmtuple</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">types</span><span class="p">):</span>
    <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;NamedTuple(&#39;Name&#39;, [(f0, t0), (f1, t1), ...]); each t must be a type&quot;</span>
    <span class="n">types</span> <span class="o">=</span> <span class="p">[(</span><span class="n">n</span><span class="p">,</span> <span class="n">_type_check</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">msg</span><span class="p">))</span> <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">types</span><span class="p">]</span>
    <span class="n">nm_tpl</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="p">[</span><span class="n">n</span> <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">types</span><span class="p">])</span>
    <span class="c1"># Prior to PEP 526, only _field_types attribute was assigned.</span>
    <span class="c1"># Now, both __annotations__ and _field_types are used to maintain compatibility.</span>
    <span class="n">nm_tpl</span><span class="o">.</span><span class="vm">__annotations__</span> <span class="o">=</span> <span class="n">nm_tpl</span><span class="o">.</span><span class="n">_field_types</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">nm_tpl</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">_getframe</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">f_globals</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;__name__&#39;</span><span class="p">,</span> <span class="s1">&#39;__main__&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">return</span> <span class="n">nm_tpl</span>


<span class="n">_PY36</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>

<span class="c1"># attributes prohibited to set in NamedTuple class syntax</span>
<span class="n">_prohibited</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__new__&#39;</span><span class="p">,</span> <span class="s1">&#39;__init__&#39;</span><span class="p">,</span> <span class="s1">&#39;__slots__&#39;</span><span class="p">,</span> <span class="s1">&#39;__getnewargs__&#39;</span><span class="p">,</span>
               <span class="s1">&#39;_fields&#39;</span><span class="p">,</span> <span class="s1">&#39;_field_defaults&#39;</span><span class="p">,</span> <span class="s1">&#39;_field_types&#39;</span><span class="p">,</span>
               <span class="s1">&#39;_make&#39;</span><span class="p">,</span> <span class="s1">&#39;_replace&#39;</span><span class="p">,</span> <span class="s1">&#39;_asdict&#39;</span><span class="p">,</span> <span class="s1">&#39;_source&#39;</span><span class="p">)</span>

<span class="n">_special</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__module__&#39;</span><span class="p">,</span> <span class="s1">&#39;__name__&#39;</span><span class="p">,</span> <span class="s1">&#39;__qualname__&#39;</span><span class="p">,</span> <span class="s1">&#39;__annotations__&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">NamedTupleMeta</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">typename</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">ns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;_root&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">typename</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">_PY36</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Class syntax for NamedTuple is only supported&quot;</span>
                            <span class="s2">&quot; in Python 3.6+&quot;</span><span class="p">)</span>
        <span class="n">types</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;__annotations__&#39;</span><span class="p">,</span> <span class="p">{})</span>
        <span class="n">nm_tpl</span> <span class="o">=</span> <span class="n">_make_nmtuple</span><span class="p">(</span><span class="n">typename</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">defaults_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">field_name</span> <span class="ow">in</span> <span class="n">types</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field_name</span> <span class="ow">in</span> <span class="n">ns</span><span class="p">:</span>
                <span class="n">default_value</span> <span class="o">=</span> <span class="n">ns</span><span class="p">[</span><span class="n">field_name</span><span class="p">]</span>
                <span class="n">defaults</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">default_value</span><span class="p">)</span>
                <span class="n">defaults_dict</span><span class="p">[</span><span class="n">field_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">default_value</span>
            <span class="k">elif</span> <span class="n">defaults</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Non-default namedtuple field </span><span class="si">{field_name}</span><span class="s2"> cannot &quot;</span>
                                <span class="s2">&quot;follow default field(s) </span><span class="si">{default_names}</span><span class="s2">&quot;</span>
                                <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">field_name</span><span class="o">=</span><span class="n">field_name</span><span class="p">,</span>
                                        <span class="n">default_names</span><span class="o">=</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">defaults_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())))</span>
        <span class="n">nm_tpl</span><span class="o">.</span><span class="fm">__new__</span><span class="o">.</span><span class="vm">__annotations__</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">(</span><span class="n">types</span><span class="p">)</span>
        <span class="n">nm_tpl</span><span class="o">.</span><span class="fm">__new__</span><span class="o">.</span><span class="vm">__defaults__</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">defaults</span><span class="p">)</span>
        <span class="n">nm_tpl</span><span class="o">.</span><span class="n">_field_defaults</span> <span class="o">=</span> <span class="n">defaults_dict</span>
        <span class="c1"># update from user namespace without overriding special namedtuple attributes</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">ns</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">_prohibited</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s2">&quot;Cannot overwrite NamedTuple attribute &quot;</span> <span class="o">+</span> <span class="n">key</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">_special</span> <span class="ow">and</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">nm_tpl</span><span class="o">.</span><span class="n">_fields</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">nm_tpl</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">ns</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">nm_tpl</span>


<span class="k">class</span> <span class="nc">NamedTuple</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">NamedTupleMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Typed version of namedtuple.</span>

<span class="sd">    Usage in Python versions &gt;= 3.6::</span>

<span class="sd">        class Employee(NamedTuple):</span>
<span class="sd">            name: str</span>
<span class="sd">            id: int</span>

<span class="sd">    This is equivalent to::</span>

<span class="sd">        Employee = collections.namedtuple(&#39;Employee&#39;, [&#39;name&#39;, &#39;id&#39;])</span>

<span class="sd">    The resulting class has extra __annotations__ and _field_types</span>
<span class="sd">    attributes, giving an ordered dict mapping field names to types.</span>
<span class="sd">    __annotations__ should be preferred, while _field_types</span>
<span class="sd">    is kept to maintain pre PEP 526 compatibility. (The field names</span>
<span class="sd">    are in the _fields attribute, which is part of the namedtuple</span>
<span class="sd">    API.) Alternative equivalent keyword syntax is also accepted::</span>

<span class="sd">        Employee = NamedTuple(&#39;Employee&#39;, name=str, id=int)</span>

<span class="sd">    In Python versions &lt;= 3.5 use::</span>

<span class="sd">        Employee = NamedTuple(&#39;Employee&#39;, [(&#39;name&#39;, str), (&#39;id&#39;, int)])</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_root</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">typename</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">kwargs</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">_PY36</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Keyword syntax for NamedTuple is only supported&quot;</span>
                            <span class="s2">&quot; in Python 3.6+&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Either list of fields or keywords&quot;</span>
                            <span class="s2">&quot; can be provided to NamedTuple, not both&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_make_nmtuple</span><span class="p">(</span><span class="n">typename</span><span class="p">,</span> <span class="n">fields</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">NewType</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">tp</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;NewType creates simple unique types with almost zero</span>
<span class="sd">    runtime overhead. NewType(name, tp) is considered a subtype of tp</span>
<span class="sd">    by static type checkers. At runtime, NewType(name, tp) returns</span>
<span class="sd">    a dummy function that simply returns its argument. Usage::</span>

<span class="sd">        UserId = NewType(&#39;UserId&#39;, int)</span>

<span class="sd">        def name_by_id(user_id: UserId) -&gt; str:</span>
<span class="sd">            ...</span>

<span class="sd">        UserId(&#39;user&#39;)          # Fails type check</span>

<span class="sd">        name_by_id(42)          # Fails type check</span>
<span class="sd">        name_by_id(UserId(42))  # OK</span>

<span class="sd">        num = UserId(5) + 1     # type: int</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">new_type</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span>

    <span class="n">new_type</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">=</span> <span class="n">name</span>
    <span class="n">new_type</span><span class="o">.</span><span class="n">__supertype__</span> <span class="o">=</span> <span class="n">tp</span>
    <span class="k">return</span> <span class="n">new_type</span>


<span class="c1"># Python-version-specific alias (Python 2: unicode; Python 3: str)</span>
<span class="n">Text</span> <span class="o">=</span> <span class="nb">str</span>


<span class="c1"># Constant that&#39;s True when type checking, but False here.</span>
<span class="n">TYPE_CHECKING</span> <span class="o">=</span> <span class="kc">False</span>


<span class="k">class</span> <span class="nc">IO</span><span class="p">(</span><span class="n">Generic</span><span class="p">[</span><span class="n">AnyStr</span><span class="p">]):</span>
    <span class="sd">&quot;&quot;&quot;Generic base class for TextIO and BinaryIO.</span>

<span class="sd">    This is an abstract, generic version of the return of open().</span>

<span class="sd">    NOTE: This does not distinguish between the different possible</span>
<span class="sd">    classes (text vs. binary, read vs. write vs. read/write,</span>
<span class="sd">    append-only, unbuffered).  The TextIO and BinaryIO subclasses</span>
<span class="sd">    below capture the distinctions between text vs. binary, which is</span>
<span class="sd">    pervasive in the interface; however we currently do not offer a</span>
<span class="sd">    way to track the other distinctions in the type system.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractproperty</span>
    <span class="k">def</span> <span class="nf">mode</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractproperty</span>
    <span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">closed</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">fileno</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">flush</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">isatty</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">AnyStr</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">readable</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">readline</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">limit</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">AnyStr</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">readlines</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hint</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">AnyStr</span><span class="p">]:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">seek</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">offset</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">whence</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">seekable</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">tell</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">truncate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">writable</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">:</span> <span class="n">AnyStr</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">writelines</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lines</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">AnyStr</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s1">&#39;IO[AnyStr]&#39;</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">BinaryIO</span><span class="p">(</span><span class="n">IO</span><span class="p">[</span><span class="nb">bytes</span><span class="p">]):</span>
    <span class="sd">&quot;&quot;&quot;Typed version of the return of open() in binary mode.&quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">bytes</span><span class="p">,</span> <span class="nb">bytearray</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s1">&#39;BinaryIO&#39;</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">TextIO</span><span class="p">(</span><span class="n">IO</span><span class="p">[</span><span class="nb">str</span><span class="p">]):</span>
    <span class="sd">&quot;&quot;&quot;Typed version of the return of open() in text mode.&quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="nd">@abstractproperty</span>
    <span class="k">def</span> <span class="nf">buffer</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">BinaryIO</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractproperty</span>
    <span class="k">def</span> <span class="nf">encoding</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractproperty</span>
    <span class="k">def</span> <span class="nf">errors</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractproperty</span>
    <span class="k">def</span> <span class="nf">line_buffering</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractproperty</span>
    <span class="k">def</span> <span class="nf">newlines</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Any</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s1">&#39;TextIO&#39;</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">io</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Wrapper namespace for IO generic classes.&quot;&quot;&quot;</span>

    <span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;IO&#39;</span><span class="p">,</span> <span class="s1">&#39;TextIO&#39;</span><span class="p">,</span> <span class="s1">&#39;BinaryIO&#39;</span><span class="p">]</span>
    <span class="n">IO</span> <span class="o">=</span> <span class="n">IO</span>
    <span class="n">TextIO</span> <span class="o">=</span> <span class="n">TextIO</span>
    <span class="n">BinaryIO</span> <span class="o">=</span> <span class="n">BinaryIO</span>


<span class="n">io</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">=</span> <span class="vm">__name__</span> <span class="o">+</span> <span class="s1">&#39;.io&#39;</span>
<span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">io</span><span class="o">.</span><span class="vm">__name__</span><span class="p">]</span> <span class="o">=</span> <span class="n">io</span>


<span class="n">Pattern</span> <span class="o">=</span> <span class="n">_TypeAlias</span><span class="p">(</span><span class="s1">&#39;Pattern&#39;</span><span class="p">,</span> <span class="n">AnyStr</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">stdlib_re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">)),</span>
                     <span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span><span class="o">.</span><span class="n">pattern</span><span class="p">)</span>
<span class="n">Match</span> <span class="o">=</span> <span class="n">_TypeAlias</span><span class="p">(</span><span class="s1">&#39;Match&#39;</span><span class="p">,</span> <span class="n">AnyStr</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">stdlib_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)),</span>
                   <span class="k">lambda</span> <span class="n">m</span><span class="p">:</span> <span class="n">m</span><span class="o">.</span><span class="n">re</span><span class="o">.</span><span class="n">pattern</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">re</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Wrapper namespace for re type aliases.&quot;&quot;&quot;</span>

    <span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Pattern&#39;</span><span class="p">,</span> <span class="s1">&#39;Match&#39;</span><span class="p">]</span>
    <span class="n">Pattern</span> <span class="o">=</span> <span class="n">Pattern</span>
    <span class="n">Match</span> <span class="o">=</span> <span class="n">Match</span>


<span class="n">re</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">=</span> <span class="vm">__name__</span> <span class="o">+</span> <span class="s1">&#39;.re&#39;</span>
<span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="vm">__name__</span><span class="p">]</span> <span class="o">=</span> <span class="n">re</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Argo AI, LLC

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>